]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2ldbm.c
Complete removal of ldapd from releng 2
[openldap] / servers / slapd / tools / ldif2ldbm.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/socket.h>
5 #include <sys/param.h>
6 #include "../slap.h"
7 #include "../back-ldbm/back-ldbm.h"
8
9 #define DEFAULT_CONFIGFILE      "%ETCDIR%/slapd.conf"
10 #define DEFAULT_ETCDIR          "%ETCDIR%"
11 #define INDEXCMD                "ldif2index"
12 #define ID2ENTRYCMD             "ldif2id2entry"
13 #define ID2CHILDRENCMD          "ldif2id2children"
14 #define MAXARGS                 100
15
16 extern void             attr_index_config();
17 extern char             *str_getline();
18 extern int              strcasecmp();
19 extern int              nbackends;
20 extern Backend          *backends;
21 extern int              ldap_debug;
22
23 int             ldap_debug;
24 int             ldap_syslog;
25 int             ldap_syslog_level;
26 int             global_schemacheck;
27 int             num_entries_sent;
28 int             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();
44 static void     wait4kids();
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] [-e etcdir]\n", name );
56         exit( 1 );
57 }
58
59 main( int argc, char **argv )
60 {
61         int             i, stop, status;
62         char            *linep, *buf, *etcdir;
63         char            *args[10];
64         char            buf2[20], buf3[20];
65         char            line[BUFSIZ];
66         char            cmd[MAXPATHLEN];
67         int             lineno, elineno;
68         int             lmax, lcur;
69         int             dbnum;
70         ID              id;
71         Backend         *be = NULL;
72         struct berval   bv;
73         struct berval   *vals[2];
74         Avlnode         *avltypes = NULL;
75         extern char     *optarg;
76
77         etcdir = DEFAULT_ETCDIR;
78         tailorfile = DEFAULT_CONFIGFILE;
79         dbnum = -1;
80         while ( (i = getopt( argc, argv, "d:e:f:i:j:n:" )) != EOF ) {
81                 switch ( i ) {
82                 case 'd':       /* turn on debugging */
83                         ldap_debug = atoi( optarg );
84                         break;
85
86                 case 'e':       /* alternate etcdir (index cmd location) */
87                         etcdir = 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 ) {
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         /*
149          * generate the id2entry index
150          */
151
152         i = 0;
153         sprintf( cmd, "%s/%s", etcdir, ID2ENTRYCMD );
154         args[i++] = cmd;
155         args[i++] = "-i";
156         args[i++] = inputfile;
157         args[i++] = "-f";
158         args[i++] = tailorfile;
159         args[i++] = "-n";
160         sprintf( buf2, "%d", dbnum );
161         args[i++] = buf2;
162         if ( ldap_debug ) {
163                 sprintf( buf3, "%d", ldap_debug );
164                 args[i++] = "-d";
165                 args[i++] = buf3;
166         }
167         args[i++] = NULL;
168         fork_child( cmd, args );
169
170         /*
171          * generate the dn2id and id2children indexes
172          */
173
174         i = 0;
175         sprintf( cmd, "%s/%s", etcdir, ID2CHILDRENCMD );
176         args[i++] = cmd;
177         args[i++] = "-i";
178         args[i++] = inputfile;
179         args[i++] = "-f";
180         args[i++] = tailorfile;
181         args[i++] = "-n";
182         sprintf( buf2, "%d", dbnum );
183         args[i++] = buf2;
184         if ( ldap_debug ) {
185                 sprintf( buf3, "%d", ldap_debug );
186                 args[i++] = "-d";
187                 args[i++] = buf3;
188         }
189         args[i++] = NULL;
190         fork_child( cmd, args );
191
192         /*
193          * generate the attribute indexes
194          */
195
196         i = 0;
197         sprintf( cmd, "%s/%s", etcdir, INDEXCMD );
198         args[i++] = cmd;
199         args[i++] = "-i";
200         args[i++] = inputfile;
201         args[i++] = "-f";
202         args[i++] = tailorfile;
203         args[i++] = "-n";
204         sprintf( buf2, "%d", dbnum );
205         args[i++] = buf2;
206         if ( ldap_debug ) {
207                 sprintf( buf3, "%d", ldap_debug );
208                 args[i++] = "-d";
209                 args[i++] = buf3;
210         }
211         args[i++] = NULL;               /* will hold the attribute name */
212         args[i++] = NULL;
213
214         id = 0;
215         stop = 0;
216         buf = NULL;
217         lineno = 0;
218         lcur = lmax = 0;
219         vals[0] = &bv;
220         vals[1] = NULL;
221         while ( ! stop ) {
222                 char            *type, *val, *s;
223                 int             vlen, indexmask, syntaxmask;
224                 Datum           key, data;
225
226                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
227                         int     len;
228
229                         lineno++;
230                         len = strlen( line );
231                         while ( lcur + len + 1 > lmax ) {
232                                 lmax += BUFSIZ;
233                                 buf = (char *) ch_realloc( buf, lmax );
234                         }
235                         strcpy( buf + lcur, line );
236                         lcur += len;
237                 } else {
238                         stop = 1;
239                 }
240                 if ( line[0] == '\n' || stop && buf && *buf ) {
241                         id++;
242                         s = buf;
243                         elineno = 0;
244                         while ( (linep = str_getline( &s )) != NULL ) {
245                                 elineno++;
246                                 if ( str_parse_line( linep, &type, &val, &vlen )
247                                     != 0 ) {
248                                         Debug( LDAP_DEBUG_PARSE,
249                             "bad line %d in entry ending at line %d ignored\n",
250                                             elineno, lineno, 0 );
251                                         continue;
252                                 }
253
254                                 if ( !isascii( *type ) || isdigit( *type ) )
255                                         continue;
256
257                                 type = strdup( type );
258                                 if ( avl_insert( &avltypes, type, strcasecmp,
259                                     avl_dup_error ) != 0 ) {
260                                         free( type );
261                                 } else {
262                                         attr_masks( be->be_private, type,
263                                             &indexmask, &syntaxmask );
264                                         if ( indexmask ) {
265                                                 args[i - 2] = type;
266                                                 fork_child( cmd, args );
267                                         }
268                                 }
269                         }
270                         *buf = '\0';
271                         lcur = 0;
272                 }
273         }
274         (*be->be_close)( be );
275
276         wait4kids( -1 );
277
278         exit( 0 );
279 }
280
281 static void
282 fork_child( char *prog, char *args[] )
283 {
284         int     status, pid;
285
286         wait4kids( maxkids );
287
288         switch ( pid = fork() ) {
289         case 0:         /* child */
290                 execvp( prog, args );
291                 fprintf( stderr, "%s: ", prog );
292                 perror( "execv" );
293                 exit( -1 );
294                 break;
295
296         case -1:        /* trouble */
297                 fprintf( stderr, "Could not fork to run %s\n", prog );
298                 perror( "fork" );
299                 break;
300
301         default:        /* parent */
302                 nkids++;
303                 break;
304         }
305 }
306
307 static void
308 wait4kids( int nkidval )
309 {
310         int             status;
311         unsigned char   *p;
312
313         while ( nkids >= nkidval ) {
314                 wait( &status );
315                 p = (unsigned char *) &status;
316                 if ( p[sizeof(int) - 1] == 0177 ) {
317                         fprintf( stderr,
318                             "stopping: child stopped with signal %d\n",
319                             p[sizeof(int) - 2] );
320                 } else if ( p[sizeof(int) - 1] != 0 ) {
321                         fprintf( stderr, 
322                             "stopping: child terminated with signal %d\n",
323                             p[sizeof(int) - 1] );
324                         exit( p[sizeof(int) - 1] );
325                 } else if ( p[sizeof(int) - 2] != 0 ) {
326                         fprintf( stderr, 
327                             "stopping: child exited with status %d\n",
328                             p[sizeof(int) - 2] );
329                         exit( p[sizeof(int) - 2] );
330                 } else {
331                         nkids--;
332                 }
333         }
334 }