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