]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2id2entry.c
Use ldapconfig.h instead of hardcoded paths.
[openldap] / servers / slapd / tools / ldif2id2entry.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 #include "ldapconfig.h"
9
10 #define MAXARGS                 100
11
12 extern struct dbcache   *ldbm_cache_open();
13 extern void             attr_index_config();
14 extern int              strcasecmp();
15 extern int              nbackends;
16 extern Backend          *backends;
17 extern int              ldap_debug;
18
19 int             ldap_debug;
20 int             ldap_syslog;
21 int             ldap_syslog_level;
22 int             global_schemacheck;
23 int             num_entries_sent;
24 int             num_bytes_sent;
25 int             active_threads;
26 char            *default_referral;
27 struct objclass *global_oc;
28 time_t          currenttime;
29 pthread_t       listener_tid;
30 pthread_mutex_t num_sent_mutex;
31 pthread_mutex_t entry2str_mutex;
32 pthread_mutex_t active_threads_mutex;
33 pthread_mutex_t new_conn_mutex;
34 pthread_mutex_t currenttime_mutex;
35 pthread_mutex_t replog_mutex;
36 pthread_mutex_t ops_mutex;
37 pthread_mutex_t regex_mutex;
38
39 static int      make_index();
40
41 static char     *tailorfile;
42 static char     *inputfile;
43  
44 static void
45 usage( char *name )
46 {
47         fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-n databasenumber]\n", name );
48         exit( 1 );
49 }
50
51 main( int argc, char **argv )
52 {
53         int             i, cargc, indb, stop, status;
54         char            *cargv[MAXARGS];
55         char            *defargv[MAXARGS];
56         char            *linep, *buf;
57         char            line[BUFSIZ], idbuf[BUFSIZ];
58         int             lmax, lcur;
59         int             dbnum;
60         ID              id;
61         struct dbcache  *db;
62         Backend         *be;
63         struct berval   bv;
64         struct berval   *vals[2];
65         Avlnode         *avltypes = NULL;
66         FILE            *fp;
67         extern char     *optarg;
68
69         tailorfile = SLAPD_DEFAULT_CONFIGFILE;
70         dbnum = -1;
71         while ( (i = getopt( argc, argv, "d:f:i:n:" )) != EOF ) {
72                 switch ( i ) {
73                 case 'd':       /* turn on debugging */
74                         ldap_debug = atoi( optarg );
75                         break;
76
77                 case 'f':       /* specify a tailor file */
78                         tailorfile = strdup( optarg );
79                         break;
80
81                 case 'i':       /* input file */
82                         inputfile = strdup( optarg );
83                         break;
84
85                 case 'n':       /* which config file db to index */
86                         dbnum = atoi( optarg ) - 1;
87                         break;
88
89                 default:
90                         usage( argv[0] );
91                         break;
92                 }
93         }
94         if ( inputfile == NULL ) {
95                 usage( argv[0] );
96         } else {
97                 if ( freopen( inputfile, "r", stdin ) == NULL ) {
98                         perror( inputfile );
99                         exit( 1 );
100                 }
101         }
102
103         /*
104          * initialize stuff and figure out which backend we're dealing with
105          */
106
107         init();
108         read_config( tailorfile, &be, NULL );
109
110         if ( dbnum == -1 ) {
111                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
112                         if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
113                             == 0 ) {
114                                 break;
115                         }
116                 }
117                 if ( dbnum == nbackends ) {
118                         fprintf( stderr, "No ldbm database found in config file\n" );
119                         exit( 1 );
120                 }
121         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
122                 fprintf( stderr, "Database number selected via -n is out of range\n" );
123                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
124                 exit( 1 );
125         } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
126                 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
127                 exit( 1 );
128         }
129         be = &backends[dbnum];
130
131         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_NEWDB ))
132             == NULL ) {
133                 perror( "id2entry file" );
134                 exit( 1 );
135         }
136
137         id = 0;
138         stop = 0;
139         buf = NULL;
140         lcur = lmax = 0;
141         vals[0] = &bv;
142         vals[1] = NULL;
143         while ( ! stop ) {
144                 char            *type, *val, *s;
145                 int             vlen;
146                 Datum           key, data;
147
148                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
149                         int     len, idlen;
150
151                         len = strlen( line );
152                         if ( buf == NULL || *buf == '\0' ) {
153                                 sprintf( idbuf, "%d\n", id + 1 );
154                                 idlen = strlen( idbuf );
155                         } else {
156                                 idlen = 0;
157                         }
158
159                         while ( lcur + len + idlen + 1 > lmax ) {
160                                 lmax += BUFSIZ;
161                                 buf = (char *) ch_realloc( buf, lmax );
162                         }
163
164                         if ( idlen > 0 ) {
165                                 strcpy( buf + lcur, idbuf );
166                                 lcur += idlen;
167                         }
168                         strcpy( buf + lcur, line );
169                         lcur += len;
170                 } else {
171                         stop = 1;
172                 }
173                 if ( line[0] == '\n' || stop && buf && *buf ) {
174                         if ( *buf != '\n' ) {
175                                 id++;
176                                 key.dptr = (char *) &id;
177                                 key.dsize = sizeof(ID);
178                                 data.dptr = buf;
179                                 data.dsize = strlen( buf ) + 1;
180                                 if ( ldbm_store( db->dbc_db, key, data,
181                                     LDBM_INSERT ) != 0 ) {
182                                         perror( "id2entry ldbm_store" );
183                                         exit( 1 );
184                                 }
185                         }
186                         *buf = '\0';
187                         lcur = 0;
188                         line[0] = '\0';
189                 }
190         }
191         (*be->be_close)( be );
192
193         id++;
194         sprintf( line, "%s/NEXTID",
195             ((struct ldbminfo *) be->be_private)->li_directory );
196         if ( (fp = fopen( line, "w" )) == NULL ) {
197                 perror( line );
198                 fprintf( stderr, "Could not write next id %ld\n", id );
199         } else {
200                 fprintf( fp, "%ld\n", id );
201                 fclose( fp );
202         }
203
204         exit( 0 );
205 }