]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2id2entry.c
391988207f5e8e448536df38489e7e43a20e9ee6
[openldap] / servers / slapd / tools / ldif2id2entry.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 #define MAXARGS                 100
16
17 int             ldap_debug;
18 int             ldap_syslog;
19 int             ldap_syslog_level;
20 long            num_entries_sent;
21 long            num_bytes_sent;
22 int             active_threads;
23 char            *default_referral;
24 time_t          currenttime;
25 pthread_t       listener_tid;
26 pthread_mutex_t num_sent_mutex;
27 pthread_mutex_t entry2str_mutex;
28 pthread_mutex_t active_threads_mutex;
29 pthread_mutex_t new_conn_mutex;
30 pthread_mutex_t currenttime_mutex;
31 pthread_mutex_t replog_mutex;
32 pthread_mutex_t ops_mutex;
33 pthread_mutex_t regex_mutex;
34 #ifdef SLAPD_CRYPT
35 pthread_mutex_t crypt_mutex;
36 #endif
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], idbuf[BUFSIZ];
56         int             lmax, lcur;
57         int             dbnum;
58         ID              id;
59         struct dbcache  *db;
60         Backend         *be = NULL;
61         struct ldbminfo *li;
62         struct berval   bv;
63         struct berval   *vals[2];
64         Avlnode         *avltypes = NULL;
65         FILE            *fp;
66
67         tailorfile = SLAPD_DEFAULT_CONFIGFILE;
68         dbnum = -1;
69         while ( (i = getopt( argc, argv, "d:f:i:n:" )) != EOF ) {
70                 switch ( i ) {
71                 case 'd':       /* turn on debugging */
72                         ldap_debug = atoi( optarg );
73                         break;
74
75                 case 'f':       /* specify a tailor file */
76                         tailorfile = strdup( optarg );
77                         break;
78
79                 case 'i':       /* input file */
80                         inputfile = strdup( optarg );
81                         break;
82
83                 case 'n':       /* which config file db to index */
84                         dbnum = atoi( optarg ) - 1;
85                         break;
86
87                 default:
88                         usage( argv[0] );
89                         break;
90                 }
91         }
92         if ( inputfile == NULL ) {
93                 usage( argv[0] );
94         } else {
95                 if ( freopen( inputfile, "r", stdin ) == NULL ) {
96                         perror( inputfile );
97                         exit( 1 );
98                 }
99         }
100
101         /*
102          * initialize stuff and figure out which backend we're dealing with
103          */
104
105         init();
106         read_config( tailorfile, &be, NULL );
107
108         if ( dbnum == -1 ) {
109                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
110                         if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
111                             == 0 ) {
112                                 break;
113                         }
114                 }
115                 if ( dbnum == nbackends ) {
116                         fprintf( stderr, "No ldbm database found in config file\n" );
117                         exit( 1 );
118                 }
119         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
120                 fprintf( stderr, "Database number selected via -n is out of range\n" );
121                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
122                 exit( 1 );
123         } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
124                 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
125                 exit( 1 );
126         }
127         be = &backends[dbnum];
128
129         /* disable write sync'ing */
130         li = (struct ldbminfo *) be->be_private;
131         li->li_dbcachewsync = 0;
132
133         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_NEWDB ))
134             == NULL ) {
135                 perror( "id2entry file" );
136                 exit( 1 );
137         }
138
139         id = 0;
140         stop = 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                 ldbm_datum_init( key );
151                 ldbm_datum_init( data );
152
153                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
154                         int     len, idlen;
155
156                         len = strlen( line );
157                         if ( buf == NULL || *buf == '\0' ) {
158                                 if (!isdigit(line[0])) {
159                                         sprintf( idbuf, "%d\n", id + 1 );
160                                         idlen = strlen( idbuf );
161                                 } else {
162                                         id = atol(line) - 1;
163                                         idlen = 0;
164                                 }
165                         } else {
166                                 idlen = 0;
167                         }
168
169                         while ( lcur + len + idlen + 1 > lmax ) {
170                                 lmax += BUFSIZ;
171                                 buf = (char *) ch_realloc( buf, lmax );
172                         }
173
174                         if ( idlen > 0 ) {
175                                 strcpy( buf + lcur, idbuf );
176                                 lcur += idlen;
177                         }
178                         strcpy( buf + lcur, line );
179                         lcur += len;
180                 } else {
181                         stop = 1;
182                 }
183                 if ( line[0] == '\n' || stop && buf && *buf ) {
184                         if ( *buf != '\n' ) {
185                                 int len;
186
187                                 id++;
188                                 key.dptr = (char *) &id;
189                                 key.dsize = sizeof(ID);
190                                 data.dptr = buf;
191                                 len = strlen(buf);
192                                 if (buf[len - 1] == '\n')
193                                         buf[--len] = '\0';
194                                 data.dsize = len + 1;
195                                 if ( ldbm_store( db->dbc_db, key, data,
196                                     LDBM_INSERT ) != 0 ) {
197                                         fputs("id2entry ldbm_store failed\n",
198                                               stderr);
199                                         exit( 1 );
200                                 }
201                         }
202                         *buf = '\0';
203                         lcur = 0;
204                         line[0] = '\0';
205                 }
206         }
207         (*be->be_close)( be );
208
209         id++;
210         sprintf( line, "%s/NEXTID",
211             ((struct ldbminfo *) be->be_private)->li_directory );
212         if ( (fp = fopen( line, "w" )) == NULL ) {
213                 perror( line );
214                 fprintf( stderr, "Could not write next id %ld\n", id );
215         } else {
216                 fprintf( fp, "%ld\n", id );
217                 fclose( fp );
218         }
219
220         exit( 0 );
221 }