]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2id2entry.c
Import unprotected strtok fix from -devel. Yes, you have to edit 8 files
[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 strtok_mutex;
32 pthread_mutex_t replog_mutex;
33 pthread_mutex_t ops_mutex;
34 pthread_mutex_t regex_mutex;
35 #ifdef SLAPD_CRYPT
36 pthread_mutex_t crypt_mutex;
37 #endif
38
39 static char     *tailorfile;
40 static char     *inputfile;
41  
42 static void
43 usage( char *name )
44 {
45         fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-n databasenumber]\n", name );
46         exit( 1 );
47 }
48
49 int
50 main( int argc, char **argv )
51 {
52         int             i, cargc, indb, stop, status;
53         char            *cargv[MAXARGS];
54         char            *defargv[MAXARGS];
55         char            *linep, *buf;
56         char            line[BUFSIZ], idbuf[BUFSIZ];
57         int             lmax, lcur;
58         int             dbnum;
59         ID              id;
60         struct dbcache  *db;
61         Backend         *be = NULL;
62         struct ldbminfo *li;
63         struct berval   bv;
64         struct berval   *vals[2];
65         Avlnode         *avltypes = NULL;
66         FILE            *fp;
67
68         tailorfile = SLAPD_DEFAULT_CONFIGFILE;
69         dbnum = -1;
70         while ( (i = getopt( argc, argv, "d:f:i:n:" )) != EOF ) {
71                 switch ( i ) {
72                 case 'd':       /* turn on debugging */
73                         ldap_debug = atoi( optarg );
74                         break;
75
76                 case 'f':       /* specify a tailor file */
77                         tailorfile = strdup( optarg );
78                         break;
79
80                 case 'i':       /* input file */
81                         inputfile = strdup( optarg );
82                         break;
83
84                 case 'n':       /* which config file db to index */
85                         dbnum = atoi( optarg ) - 1;
86                         break;
87
88                 default:
89                         usage( argv[0] );
90                         break;
91                 }
92         }
93         if ( inputfile == NULL ) {
94                 usage( argv[0] );
95         } else {
96                 if ( freopen( inputfile, "r", stdin ) == NULL ) {
97                         perror( inputfile );
98                         exit( 1 );
99                 }
100         }
101
102         /*
103          * initialize stuff and figure out which backend we're dealing with
104          */
105
106         init();
107         read_config( tailorfile, &be, NULL );
108
109         if ( dbnum == -1 ) {
110                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
111                         if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
112                             == 0 ) {
113                                 break;
114                         }
115                 }
116                 if ( dbnum == nbackends ) {
117                         fprintf( stderr, "No ldbm database found in config file\n" );
118                         exit( 1 );
119                 }
120         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
121                 fprintf( stderr, "Database number selected via -n is out of range\n" );
122                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
123                 exit( 1 );
124         } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
125                 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
126                 exit( 1 );
127         }
128         be = &backends[dbnum];
129
130         /* disable write sync'ing */
131         li = (struct ldbminfo *) be->be_private;
132         li->li_dbcachewsync = 0;
133
134         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_NEWDB ))
135             == NULL ) {
136                 perror( "id2entry file" );
137                 exit( 1 );
138         }
139
140         id = 0;
141         stop = 0;
142         buf = NULL;
143         lcur = lmax = 0;
144         vals[0] = &bv;
145         vals[1] = NULL;
146         while ( ! stop ) {
147                 char            *type, *val, *s;
148                 int             vlen;
149                 Datum           key, data;
150
151                 ldbm_datum_init( key );
152                 ldbm_datum_init( data );
153
154                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
155                         int     len, idlen;
156
157                         len = strlen( line );
158                         if ( buf == NULL || *buf == '\0' ) {
159                                 if (!isdigit(line[0])) {
160                                         sprintf( idbuf, "%d\n", id + 1 );
161                                         idlen = strlen( idbuf );
162                                 } else {
163                                         id = atol(line) - 1;
164                                         idlen = 0;
165                                 }
166                         } else {
167                                 idlen = 0;
168                         }
169
170                         while ( lcur + len + idlen + 1 > lmax ) {
171                                 lmax += BUFSIZ;
172                                 buf = (char *) ch_realloc( buf, lmax );
173                         }
174
175                         if ( idlen > 0 ) {
176                                 strcpy( buf + lcur, idbuf );
177                                 lcur += idlen;
178                         }
179                         strcpy( buf + lcur, line );
180                         lcur += len;
181                 } else {
182                         stop = 1;
183                 }
184                 if ( line[0] == '\n' || stop && buf && *buf ) {
185                         if ( *buf != '\n' ) {
186                                 int len;
187
188                                 id++;
189                                 key.dptr = (char *) &id;
190                                 key.dsize = sizeof(ID);
191                                 data.dptr = buf;
192                                 len = strlen(buf);
193                                 if (buf[len - 1] == '\n')
194                                         buf[--len] = '\0';
195                                 data.dsize = len + 1;
196                                 if ( ldbm_store( db->dbc_db, key, data,
197                                     LDBM_INSERT ) != 0 ) {
198                                         fputs("id2entry ldbm_store failed\n",
199                                               stderr);
200                                         exit( 1 );
201                                 }
202                         }
203                         *buf = '\0';
204                         lcur = 0;
205                         line[0] = '\0';
206                 }
207         }
208         (*be->be_close)( be );
209
210         id++;
211         sprintf( line, "%s/NEXTID",
212             ((struct ldbminfo *) be->be_private)->li_directory );
213         if ( (fp = fopen( line, "w" )) == NULL ) {
214                 perror( line );
215                 fprintf( stderr, "Could not write next id %ld\n", id );
216         } else {
217                 fprintf( fp, "%ld\n", id );
218                 fclose( fp );
219         }
220
221         exit( 0 );
222 }