]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2index.c
Import unprotected strtok fix from -devel. Yes, you have to edit 8 files
[openldap] / servers / slapd / tools / ldif2index.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 "../slap.h"
12 #include "../back-ldbm/back-ldbm.h"
13
14 #include "ldapconfig.h"
15 #include "ldif.h"
16
17 #define MAXARGS                 100
18
19 int             ldap_debug;
20 int             ldap_syslog;
21 int             ldap_syslog_level;
22 long            num_entries_sent;
23 long            num_bytes_sent;
24 int             active_threads;
25 char            *default_referral;
26 time_t          currenttime;
27 pthread_t       listener_tid;
28 pthread_mutex_t num_sent_mutex;
29 pthread_mutex_t entry2str_mutex;
30 pthread_mutex_t active_threads_mutex;
31 pthread_mutex_t new_conn_mutex;
32 pthread_mutex_t currenttime_mutex;
33 pthread_mutex_t strtok_mutex;
34 pthread_mutex_t replog_mutex;
35 pthread_mutex_t ops_mutex;
36 pthread_mutex_t regex_mutex;
37 #ifdef SLAPD_CRYPT
38 pthread_mutex_t crypt_mutex;
39 #endif
40
41
42 static void
43 usage( char *name )
44 {
45         fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-n databasenumber] attr\n", name );
46         exit( 1 );
47 }
48
49 int
50 main( int argc, char **argv )
51 {
52         int             i, cargc, indb, stop;
53         char            *cargv[MAXARGS];
54         char            *defargv[MAXARGS];
55         char            *tailorfile, *inputfile;
56         char            *linep, *buf, *attr;
57         char            line[BUFSIZ];
58         int             lineno, elineno;
59         int             lmax, lcur, indexmask, syntaxmask;
60         int             dbnum;
61         unsigned long   id;
62         Backend         *be = NULL;
63         struct ldbminfo *li;
64         struct berval   bv;
65         struct berval   *vals[2];
66
67         inputfile = NULL;
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         attr = attr_normalize( argv[argc - 1] );
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         init();
104         read_config( tailorfile, &be, NULL );
105
106         if ( dbnum == -1 ) {
107                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
108                         if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
109                             == 0 ) {
110                                 break;
111                         }
112                 }
113                 if ( dbnum == nbackends ) {
114                         fprintf( stderr, "No ldbm database found in config file\n" );
115                         exit( 1 );
116                 }
117         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
118                 fprintf( stderr, "Database number selected via -n is out of range\n" );
119                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
120                 exit( 1 );
121         } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
122                 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
123                 exit( 1 );
124         }
125         be = &backends[dbnum];
126
127         /* disable write sync'ing */
128         li = (struct ldbminfo *) be->be_private;
129         li->li_dbcachewsync = 0;
130
131         attr_masks( be->be_private, attr, &indexmask, &syntaxmask );
132         if ( indexmask == 0 ) {
133                 exit( 0 );
134         }
135
136         id = 0;
137         stop = 0;
138         lineno = 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
147                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
148                         int     len;
149
150                         lineno++;
151                         len = strlen( line );
152                         while ( lcur + len + 1 > lmax ) {
153                                 lmax += BUFSIZ;
154                                 buf = (char *) ch_realloc( buf, lmax );
155                         }
156                         strcpy( buf + lcur, line );
157                         lcur += len;
158                 } else {
159                         stop = 1;
160                 }
161                 if ( line[0] == '\n' || stop && buf && *buf ) {
162                         if ( *buf != '\n' ) {
163                                 if (isdigit(*buf)) {
164                                         id = atol(buf);
165                                 } else {
166                                         id++;
167                                 }
168                                 s = buf;
169                                 elineno = 0;
170                                 while ( (linep = str_getline( &s )) != NULL ) {
171                                         elineno++;
172                                         if ( str_parse_line( linep, &type, &val,
173                                             &vlen ) != 0 ) {
174                                                 Debug( LDAP_DEBUG_PARSE,
175                             "bad line %d in entry ending at line %d ignored\n",
176                                                     elineno, elineno, 0 );
177                                                 continue;
178                                         }
179
180                                         if ( strcasecmp( type, attr ) == 0 ) {
181                                                 bv.bv_val = val;
182                                                 bv.bv_len = vlen;
183                                                 index_add_values( be, attr,
184                                                     vals, id );
185                                         }
186                                 }
187                         }
188                         *buf = '\0';
189                         lcur = 0;
190                 }
191         }
192         (*be->be_close)( be );
193
194         exit( 0 );
195 }