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