]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2ldbm.c
Remove duplicate defs of global_schemacheck and global_oc (from devel)
[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 long            num_entries_sent;
27 long            num_bytes_sent;
28 int             active_threads;
29 char            *default_referral;
30 time_t          currenttime;
31 pthread_t       listener_tid;
32 pthread_mutex_t num_sent_mutex;
33 pthread_mutex_t entry2str_mutex;
34 pthread_mutex_t active_threads_mutex;
35 pthread_mutex_t new_conn_mutex;
36 pthread_mutex_t currenttime_mutex;
37 pthread_mutex_t replog_mutex;
38 pthread_mutex_t ops_mutex;
39 pthread_mutex_t regex_mutex;
40
41 static void fork_child( char *prog, char *args[] );
42 static void     wait4kids( int nkidval );
43
44 static char     *indexcmd;
45 static char     *tailorfile;
46 static char     *inputfile;
47 static int      maxkids = 1;
48 static int      nkids;
49
50 static void
51 usage( char *name )
52 {
53         fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-j #jobs] [-n databasenumber] [-s sbindir]\n", name );
54         exit( 1 );
55 }
56
57 int
58 main( int argc, char **argv )
59 {
60         int             i, stop, status;
61         char            *linep, *buf, *sbindir;
62         char            *args[10];
63         char            buf2[20], buf3[20];
64         char            line[BUFSIZ];
65         char            cmd[MAXPATHLEN];
66         int             lineno, elineno;
67         int             lmax, lcur;
68         int             dbnum;
69         ID              id;
70         Backend         *be = NULL;
71         struct ldbminfo *li;
72         struct berval   bv;
73         struct berval   *vals[2];
74         Avlnode         *avltypes = NULL;
75
76         sbindir = DEFAULT_SBINDIR;
77         tailorfile = SLAPD_DEFAULT_CONFIGFILE;
78         dbnum = -1;
79         while ( (i = getopt( argc, argv, "d:e:s:f:i:j:n:" )) != EOF ) {
80                 switch ( i ) {
81                 case 'd':       /* turn on debugging */
82                         ldap_debug = atoi( optarg );
83                         break;
84
85                 case 's':       /* alternate sbindir (index cmd location) */
86                 case 'e':       /* accept -e for backwards compatibility */
87                         sbindir = strdup( optarg );
88                         break;
89
90                 case 'f':       /* specify a tailor file */
91                         tailorfile = strdup( optarg );
92                         break;
93
94                 case 'i':       /* input file */
95                         inputfile = strdup( optarg );
96                         break;
97
98                 case 'j':       /* number of parallel index procs */
99                         maxkids = atoi( optarg );
100                         break;
101
102                 case 'n':       /* which config file db to index */
103                         dbnum = atoi( optarg ) - 1;
104                         break;
105
106                 default:
107                         usage( argv[0] );
108                         break;
109                 }
110         }
111         if ( inputfile == NULL ) {
112                 usage( argv[0] );
113         } else {
114                 if ( freopen( inputfile, "r", stdin ) == NULL ) {
115                         perror( inputfile );
116                         exit( 1 );
117                 }
118         }
119
120         /*
121          * initialize stuff and figure out which backend we're dealing with
122          */
123
124         init();
125         read_config( tailorfile, &be, NULL );
126
127         if ( dbnum == -1 ) {
128                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
129                         if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
130                             == 0 ) {
131                                 break;
132                         }
133                 }
134                 if ( dbnum == nbackends ) {
135                         fprintf( stderr, "No ldbm database found in config file\n" );
136                         exit( 1 );
137                 }
138         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
139                 fprintf( stderr, "Database number selected via -n is out of range\n" );
140                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
141                 exit( 1 );
142         } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
143                 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
144                 exit( 1 );
145         }
146         be = &backends[dbnum];
147
148         /* disable write sync'ing */
149         li = (struct ldbminfo *) be->be_private;
150         li->li_dbcachewsync = 0;
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                 memset( &key, 0, sizeof( key ));
231                 memset( &data, 0, sizeof( data ));
232
233                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
234                         int     len;
235
236                         lineno++;
237                         len = strlen( line );
238                         while ( lcur + len + 1 > lmax ) {
239                                 lmax += BUFSIZ;
240                                 buf = (char *) ch_realloc( buf, lmax );
241                         }
242                         strcpy( buf + lcur, line );
243                         lcur += len;
244                 } else {
245                         stop = 1;
246                 }
247                 if ( line[0] == '\n' || stop && buf && *buf ) {
248                         id++;
249                         s = buf;
250                         elineno = 0;
251                         while ( (linep = str_getline( &s )) != NULL ) {
252                                 elineno++;
253                                 if ( str_parse_line( linep, &type, &val, &vlen )
254                                     != 0 ) {
255                                         Debug( LDAP_DEBUG_PARSE,
256                             "bad line %d in entry ending at line %d ignored\n",
257                                             elineno, lineno, 0 );
258                                         continue;
259                                 }
260
261                                 if ( !isascii( *type ) || isdigit( *type ) )
262                                         continue;
263
264                                 type = strdup( type );
265                                 if ( avl_insert( &avltypes, type, strcasecmp,
266                                     avl_dup_error ) != 0 ) {
267                                         free( type );
268                                 } else {
269                                         attr_masks( be->be_private, type,
270                                             &indexmask, &syntaxmask );
271                                         if ( indexmask ) {
272                                                 args[i - 2] = type;
273                                                 fork_child( cmd, args );
274                                         }
275                                 }
276                         }
277                         *buf = '\0';
278                         lcur = 0;
279                 }
280         }
281         (*be->be_close)( be );
282
283         wait4kids( -1 );
284
285         exit( 0 );
286 }
287
288 static void
289 fork_child( char *prog, char *args[] )
290 {
291         int     status, pid;
292
293         wait4kids( maxkids );
294
295         switch ( pid = fork() ) {
296         case 0:         /* child */
297                 execvp( prog, args );
298                 fprintf( stderr, "%s: ", prog );
299                 perror( "execv" );
300                 exit( -1 );
301                 break;
302
303         case -1:        /* trouble */
304                 fprintf( stderr, "Could not fork to run %s\n", prog );
305                 perror( "fork" );
306                 break;
307
308         default:        /* parent */
309                 nkids++;
310                 break;
311         }
312 }
313
314 static void
315 wait4kids( int nkidval )
316 {
317         int             status;
318         unsigned char   *p;
319
320         while ( nkids >= nkidval ) {
321                 wait( &status );
322                 p = (unsigned char *) &status;
323                 if ( p[sizeof(int) - 1] == 0177 ) {
324                         fprintf( stderr,
325                             "stopping: child stopped with signal %d\n",
326                             p[sizeof(int) - 2] );
327                 } else if ( p[sizeof(int) - 1] != 0 ) {
328                         fprintf( stderr, 
329                             "stopping: child terminated with signal %d\n",
330                             p[sizeof(int) - 1] );
331                         exit( p[sizeof(int) - 1] );
332                 } else if ( p[sizeof(int) - 2] != 0 ) {
333                         fprintf( stderr, 
334                             "stopping: child exited with status %d\n",
335                             p[sizeof(int) - 2] );
336                         exit( p[sizeof(int) - 2] );
337                 } else {
338                         nkids--;
339                 }
340         }
341 }