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