]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2id2children.c
5e709dc9bc44d5387f9784823d4563b82d7dc57d
[openldap] / servers / slapd / tools / ldif2id2children.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/string.h>
6 #include <ac/socket.h>
7 #include <ac/unistd.h>
8
9 #include "ldapconfig.h"
10 #include "../slap.h"
11 #include "../back-ldbm/back-ldbm.h"
12
13 #include "ldif.h"
14
15 #define MAXARGS                 100
16
17 int             lineno;
18 int             ldap_debug;
19 int             ldap_syslog;
20 int             ldap_syslog_level;
21 int             global_schemacheck;
22 long            num_entries_sent;
23 long            num_bytes_sent;
24 int             active_threads;
25 char            *default_referral;
26 struct objclass *global_oc;
27 time_t          currenttime;
28 pthread_t       listener_tid;
29 pthread_mutex_t num_sent_mutex;
30 pthread_mutex_t entry2str_mutex;
31 pthread_mutex_t active_threads_mutex;
32 pthread_mutex_t new_conn_mutex;
33 pthread_mutex_t currenttime_mutex;
34 pthread_mutex_t replog_mutex;
35 pthread_mutex_t ops_mutex;
36 pthread_mutex_t regex_mutex;
37
38 static char     *tailorfile;
39 static char     *inputfile;
40  
41 static void
42 usage( char *name )
43 {
44         fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-n databasenumber]\n", name );
45         exit( 1 );
46 }
47
48 int
49 main( int argc, char **argv )
50 {
51         int             i, cargc, indb, stop, status;
52         char            *cargv[MAXARGS];
53         char            *defargv[MAXARGS];
54         char            *linep, *buf;
55         char            line[BUFSIZ];
56         int             lineno, elineno;
57         int             lmax, lcur;
58         int             dbnum;
59         ID              id;
60         struct dbcache  *db, *db2;
61         Backend         *be = NULL;
62         struct berval   bv;
63         struct berval   *vals[2];
64         Avlnode         *avltypes = NULL;
65
66         tailorfile = SLAPD_DEFAULT_CONFIGFILE;
67         dbnum = -1;
68         while ( (i = getopt( argc, argv, "d:f:i:n:" )) != EOF ) {
69                 switch ( i ) {
70                 case 'd':       /* turn on debugging */
71                         ldap_debug = atoi( optarg );
72                         break;
73
74                 case 'f':       /* specify a tailor file */
75                         tailorfile = strdup( optarg );
76                         break;
77
78                 case 'i':       /* input file */
79                         inputfile = strdup( optarg );
80                         break;
81
82                 case 'n':       /* which config file db to index */
83                         dbnum = atoi( optarg ) - 1;
84                         break;
85
86                 default:
87                         usage( argv[0] );
88                         break;
89                 }
90         }
91         if ( inputfile == NULL ) {
92                 usage( argv[0] );
93         } else {
94                 if ( freopen( inputfile, "r", stdin ) == NULL ) {
95                         perror( inputfile );
96                         exit( 1 );
97                 }
98         }
99
100         /*
101          * initialize stuff and figure out which backend we're dealing with
102          */
103
104         init();
105         read_config( tailorfile, &be, NULL );
106
107         if ( dbnum == -1 ) {
108                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
109                         if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
110                             == 0 ) {
111                                 break;
112                         }
113                 }
114                 if ( dbnum == nbackends ) {
115                         fprintf( stderr, "No ldbm database found in config file\n" );
116                         exit( 1 );
117                 }
118         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
119                 fprintf( stderr, "Database number selected via -n is out of range\n" );
120                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
121                 exit( 1 );
122         } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
123                 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
124                 exit( 1 );
125         }
126         be = &backends[dbnum];
127
128         /*
129          * first, make the dn2id index
130          */
131
132         if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_NEWDB ))
133             == NULL ) {
134                 perror( "dn2id file" );
135                 exit( 1 );
136         }
137
138         id = 0;
139         stop = 0;
140         lineno = 0;
141         buf = NULL;
142         lcur = lmax = 0;
143         vals[0] = &bv;
144         vals[1] = NULL;
145         while ( ! stop ) {
146                 char            *type, *val, *s;
147                 int             vlen;
148                 Datum           key, data;
149
150                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
151                         int     len;
152
153                         lineno++;
154                         len = strlen( line );
155                         while ( lcur + len + 1 > lmax ) {
156                                 lmax += BUFSIZ;
157                                 buf = (char *) ch_realloc( buf, lmax );
158                         }
159                         strcpy( buf + lcur, line );
160                         lcur += len;
161                 } else {
162                         stop = 1;
163                 }
164                 if ( line[0] == '\n' || stop && buf && *buf ) {
165                         if ( *buf != '\n' ) {
166                                 id++;
167                                 s = buf;
168                                 elineno = 0;
169                                 while ( (linep = str_getline( &s )) != NULL ) {
170                                         elineno++;
171                                         if ( str_parse_line( linep, &type, &val,
172                                             &vlen ) != 0 ) {
173                                                 Debug( LDAP_DEBUG_PARSE,
174                             "bad line %d in entry ending at line %d ignored\n",
175                                                     elineno, lineno, 0 );
176                                                 continue;
177                                         }
178
179                                         if ( strcmp( type, "dn" ) == 0 )
180                                                 break;
181                                 }
182
183                                 if ( linep == NULL ) {
184                                         fprintf( stderr, "entry %lu has no dn\n",
185                                             id );
186                                 } else {
187                                         key.dptr = dn_normalize_case( val );
188                                         key.dsize = strlen( val ) + 1;
189                                         data.dptr = (char *) &id;
190                                         data.dsize = sizeof(ID);
191                                         if ( ldbm_store( db->dbc_db, key, data,
192                                             LDBM_REPLACE ) != 0 ) {
193                                                 perror( "dn2id ldbm_store" );
194                                                 exit( 1 );
195                                         }
196                                 }
197                         }
198                         *buf = '\0';
199                         lcur = 0;
200                         line[0] = '\0';
201                 }
202         }
203
204         /*
205          * next, make the id2children index
206          */
207
208         if ( (db2 = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
209             LDBM_NEWDB )) == NULL ) {
210                 perror( "id2children file" );
211                 exit( 1 );
212         }
213
214         rewind( stdin );
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, *dn;
224                 int     vlen;
225                 ID      pid;
226                 char    buf2[20];
227                 Datum   key, data;
228
229                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
230                         int     len;
231
232                         len = strlen( line );
233                         while ( lcur + len + 1 > lmax ) {
234                                 lmax += BUFSIZ;
235                                 buf = (char *) ch_realloc( buf, lmax );
236                         }
237                         strcpy( buf + lcur, line );
238                         lcur += len;
239                 } else {
240                         stop = 1;
241                 }
242                 if ( line[0] == '\n' || stop && buf && *buf ) {
243                         if ( * buf != '\n' ) {
244                                 id++;
245                                 s = buf;
246                                 while ( (linep = str_getline( &s )) != NULL ) {
247                                         if ( str_parse_line( linep, &type, &val,
248                                             &vlen ) != 0 ) {
249                                                 Debug( LDAP_DEBUG_PARSE,
250                                                     "bad line %d ignored\n",
251                                                     lineno, 0, 0 );
252                                                 continue;
253                                         }
254
255                                         if ( strcmp( type, "dn" ) == 0 )
256                                                 break;
257                                 }
258
259                                 if ( linep == NULL ) {
260                                         fprintf( stderr, "entry %lu has no dn\n",
261                                             id );
262                                 } else {
263                                         if ( (dn = dn_parent( be, val ))
264                                             == NULL ) {
265                                                 pid = 0;
266                                         } else {
267                                                 key.dptr =
268                                                     dn_normalize_case( dn );
269                                                 key.dsize = strlen( dn ) + 1;
270
271                                                 data = ldbm_fetch( db->dbc_db,
272                                                     key );
273                                                 free( dn );
274                                                 if ( data.dptr == NULL ) {
275                                                         dn_normalize( val );
276                                                         if ( ! be_issuffix( be,
277                                                             val ) ) {
278         Debug( LDAP_DEBUG_PARSE, "no parent \"%s\" of \"%s\"\n", dn, val, 0 );
279                                                         }
280                                                         *buf = '\0';
281                                                         lcur = 0;
282                                                         line[0] = '\0';
283                                                         continue;
284                                                 }
285                                                 (void) memcpy( (char *) &pid,
286                                                     data.dptr, sizeof(ID) );
287                                         }
288
289                                         sprintf( buf2, "%c%ld", EQ_PREFIX, pid );
290                                         key.dptr = buf2;
291                                         key.dsize = strlen( buf2 ) + 1;
292                                         if ( idl_insert_key( be, db2, key, id )
293                                             != 0 ) {
294                                                 perror( "idl_insert_key" );
295                                                 exit( 1 );
296                                         }
297                                 }
298                         }
299                         *buf = '\0';
300                         lcur = 0;
301                         line[0] = '\0';
302                 }
303         }
304         (*be->be_close)( be );
305
306         exit( 0 );
307 }