]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2ldbm.c
Import KRB_LIBS fix 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 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                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
228                         int     len;
229
230                         lineno++;
231                         len = strlen( line );
232                         while ( lcur + len + 1 > lmax ) {
233                                 lmax += BUFSIZ;
234                                 buf = (char *) ch_realloc( buf, lmax );
235                         }
236                         strcpy( buf + lcur, line );
237                         lcur += len;
238                 } else {
239                         stop = 1;
240                 }
241                 if ( line[0] == '\n' || stop && buf && *buf ) {
242                         id++;
243                         s = buf;
244                         elineno = 0;
245                         while ( (linep = str_getline( &s )) != NULL ) {
246                                 elineno++;
247                                 if ( str_parse_line( linep, &type, &val, &vlen )
248                                     != 0 ) {
249                                         Debug( LDAP_DEBUG_PARSE,
250                             "bad line %d in entry ending at line %d ignored\n",
251                                             elineno, lineno, 0 );
252                                         continue;
253                                 }
254
255                                 if ( !isascii( *type ) || isdigit( *type ) )
256                                         continue;
257
258                                 type = strdup( type );
259                                 if ( avl_insert( &avltypes, type, strcasecmp,
260                                     avl_dup_error ) != 0 ) {
261                                         free( type );
262                                 } else {
263                                         attr_masks( be->be_private, type,
264                                             &indexmask, &syntaxmask );
265                                         if ( indexmask ) {
266                                                 args[i - 2] = type;
267                                                 fork_child( cmd, args );
268                                         }
269                                 }
270                         }
271                         *buf = '\0';
272                         lcur = 0;
273                 }
274         }
275         (*be->be_close)( be );
276
277         wait4kids( -1 );
278
279         exit( 0 );
280 }
281
282 static void
283 fork_child( char *prog, char *args[] )
284 {
285         int     status, pid;
286
287         wait4kids( maxkids );
288
289         switch ( pid = fork() ) {
290         case 0:         /* child */
291                 execvp( prog, args );
292                 fprintf( stderr, "%s: ", prog );
293                 perror( "execv" );
294                 exit( -1 );
295                 break;
296
297         case -1:        /* trouble */
298                 fprintf( stderr, "Could not fork to run %s\n", prog );
299                 perror( "fork" );
300                 break;
301
302         default:        /* parent */
303                 nkids++;
304                 break;
305         }
306 }
307
308 static void
309 wait4kids( int nkidval )
310 {
311         int             status;
312         unsigned char   *p;
313
314         while ( nkids >= nkidval ) {
315                 wait( &status );
316                 p = (unsigned char *) &status;
317                 if ( p[sizeof(int) - 1] == 0177 ) {
318                         fprintf( stderr,
319                             "stopping: child stopped with signal %d\n",
320                             p[sizeof(int) - 2] );
321                 } else if ( p[sizeof(int) - 1] != 0 ) {
322                         fprintf( stderr, 
323                             "stopping: child terminated with signal %d\n",
324                             p[sizeof(int) - 1] );
325                         exit( p[sizeof(int) - 1] );
326                 } else if ( p[sizeof(int) - 2] != 0 ) {
327                         fprintf( stderr, 
328                             "stopping: child exited with status %d\n",
329                             p[sizeof(int) - 2] );
330                         exit( p[sizeof(int) - 2] );
331                 } else {
332                         nkids--;
333                 }
334         }
335 }