]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2ldbm.c
Import unprotected strtok fix from -devel. Yes, you have to edit 8 files
[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 strtok_mutex;
38 pthread_mutex_t replog_mutex;
39 pthread_mutex_t ops_mutex;
40 pthread_mutex_t regex_mutex;
41 #ifdef SLAPD_CRYPT
42 pthread_mutex_t crypt_mutex;
43 #endif
44
45 static void fork_child( char *prog, char *args[] );
46 static void     wait4kids( int nkidval );
47
48 static char     *indexcmd;
49 static char     *tailorfile;
50 static char     *inputfile;
51 static int      maxkids = 1;
52 static int      nkids;
53
54 static void
55 usage( char *name )
56 {
57         fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-j #jobs] [-n databasenumber] [-s sbindir]\n", name );
58         exit( 1 );
59 }
60
61 int
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 ldbminfo *li;
76         struct berval   bv;
77         struct berval   *vals[2];
78         Avlnode         *avltypes = NULL;
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         /* disable write sync'ing */
153         li = (struct ldbminfo *) be->be_private;
154         li->li_dbcachewsync = 0;
155
156         /*
157          * generate the id2entry index
158          */
159
160         i = 0;
161         sprintf( cmd, "%s/%s", sbindir, ID2ENTRYCMD );
162         args[i++] = cmd;
163         args[i++] = "-i";
164         args[i++] = inputfile;
165         args[i++] = "-f";
166         args[i++] = tailorfile;
167         args[i++] = "-n";
168         sprintf( buf2, "%d", dbnum+1 );
169         args[i++] = buf2;
170         if ( ldap_debug ) {
171                 sprintf( buf3, "%d", ldap_debug );
172                 args[i++] = "-d";
173                 args[i++] = buf3;
174         }
175         args[i++] = NULL;
176         fork_child( cmd, args );
177
178         /*
179          * generate the dn2id and id2children indexes
180          */
181
182         i = 0;
183         sprintf( cmd, "%s/%s", sbindir, ID2CHILDRENCMD );
184         args[i++] = cmd;
185         args[i++] = "-i";
186         args[i++] = inputfile;
187         args[i++] = "-f";
188         args[i++] = tailorfile;
189         args[i++] = "-n";
190         sprintf( buf2, "%d", dbnum+1 );
191         args[i++] = buf2;
192         if ( ldap_debug ) {
193                 sprintf( buf3, "%d", ldap_debug );
194                 args[i++] = "-d";
195                 args[i++] = buf3;
196         }
197         args[i++] = NULL;
198         fork_child( cmd, args );
199
200         /*
201          * generate the attribute indexes
202          */
203
204         i = 0;
205         sprintf( cmd, "%s/%s", sbindir, INDEXCMD );
206         args[i++] = cmd;
207         args[i++] = "-i";
208         args[i++] = inputfile;
209         args[i++] = "-f";
210         args[i++] = tailorfile;
211         args[i++] = "-n";
212         sprintf( buf2, "%d", dbnum+1 );
213         args[i++] = buf2;
214         if ( ldap_debug ) {
215                 sprintf( buf3, "%d", ldap_debug );
216                 args[i++] = "-d";
217                 args[i++] = buf3;
218         }
219         args[i++] = NULL;               /* will hold the attribute name */
220         args[i++] = NULL;
221
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;
231                 int             vlen, indexmask, syntaxmask;
232                 Datum           key, data;
233
234                 ldbm_datum_init( key );
235                 ldbm_datum_init( data );
236
237                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
238                         int     len;
239
240                         lineno++;
241                         len = strlen( line );
242                         while ( lcur + len + 1 > lmax ) {
243                                 lmax += BUFSIZ;
244                                 buf = (char *) ch_realloc( buf, lmax );
245                         }
246                         strcpy( buf + lcur, line );
247                         lcur += len;
248                 } else {
249                         stop = 1;
250                 }
251                 if ( line[0] == '\n' || stop && buf && *buf ) {
252                         id++;
253                         s = buf;
254                         elineno = 0;
255                         while ( (linep = str_getline( &s )) != NULL ) {
256                                 elineno++;
257                                 if ( str_parse_line( linep, &type, &val, &vlen )
258                                     != 0 ) {
259                                         Debug( LDAP_DEBUG_PARSE,
260                             "bad line %d in entry ending at line %d ignored\n",
261                                             elineno, lineno, 0 );
262                                         continue;
263                                 }
264
265                                 if ( !isascii( *type ) || isdigit( *type ) )
266                                         continue;
267
268                                 type = strdup( type );
269                                 if ( avl_insert( &avltypes, type, strcasecmp,
270                                     avl_dup_error ) != 0 ) {
271                                         free( type );
272                                 } else {
273                                         attr_masks( be->be_private, type,
274                                             &indexmask, &syntaxmask );
275                                         if ( indexmask ) {
276                                                 args[i - 2] = type;
277                                                 fork_child( cmd, args );
278                                         }
279                                 }
280                         }
281                         *buf = '\0';
282                         lcur = 0;
283                 }
284         }
285         (*be->be_close)( be );
286
287         wait4kids( -1 );
288
289         exit( 0 );
290 }
291
292 static void
293 fork_child( char *prog, char *args[] )
294 {
295         int     status, pid;
296
297         wait4kids( maxkids );
298
299         switch ( pid = fork() ) {
300         case 0:         /* child */
301                 execvp( prog, args );
302                 fprintf( stderr, "%s: ", prog );
303                 perror( "execv" );
304                 exit( -1 );
305                 break;
306
307         case -1:        /* trouble */
308                 fprintf( stderr, "Could not fork to run %s\n", prog );
309                 perror( "fork" );
310                 break;
311
312         default:        /* parent */
313                 nkids++;
314                 break;
315         }
316 }
317
318 static void
319 wait4kids( int nkidval )
320 {
321         int             status;
322         unsigned char   *p;
323
324         while ( nkids >= nkidval ) {
325                 wait( &status );
326                 p = (unsigned char *) &status;
327                 if ( p[sizeof(int) - 1] == 0177 ) {
328                         fprintf( stderr,
329                             "stopping: child stopped with signal %d\n",
330                             p[sizeof(int) - 2] );
331                 } else if ( p[sizeof(int) - 1] != 0 ) {
332                         fprintf( stderr, 
333                             "stopping: child terminated with signal %d\n",
334                             p[sizeof(int) - 1] );
335                         exit( p[sizeof(int) - 1] );
336                 } else if ( p[sizeof(int) - 2] != 0 ) {
337                         fprintf( stderr, 
338                             "stopping: child exited with status %d\n",
339                             p[sizeof(int) - 2] );
340                         exit( p[sizeof(int) - 2] );
341                 } else {
342                         nkids--;
343                 }
344         }
345 }