]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2index.c
Apply --disable-crypt & -disable-cleartext fixes from devel.
[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 replog_mutex;
34 pthread_mutex_t ops_mutex;
35 pthread_mutex_t regex_mutex;
36 #ifdef SLAPD_CRYPT
37 pthread_mutex_t crypt_mutex;
38 #endif
39
40
41 static void
42 usage( char *name )
43 {
44         fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-n databasenumber] attr\n", name );
45         exit( 1 );
46 }
47
48 int
49 main( int argc, char **argv )
50 {
51         int             i, cargc, indb, stop;
52         char            *cargv[MAXARGS];
53         char            *defargv[MAXARGS];
54         char            *tailorfile, *inputfile;
55         char            *linep, *buf, *attr;
56         char            line[BUFSIZ];
57         int             lineno, elineno;
58         int             lmax, lcur, indexmask, syntaxmask;
59         int             dbnum;
60         unsigned long   id;
61         Backend         *be = NULL;
62         struct ldbminfo *li;
63         struct berval   bv;
64         struct berval   *vals[2];
65
66         inputfile = NULL;
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         attr = attr_normalize( argv[argc - 1] );
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         init();
103         read_config( tailorfile, &be, NULL );
104
105         if ( dbnum == -1 ) {
106                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
107                         if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
108                             == 0 ) {
109                                 break;
110                         }
111                 }
112                 if ( dbnum == nbackends ) {
113                         fprintf( stderr, "No ldbm database found in config file\n" );
114                         exit( 1 );
115                 }
116         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
117                 fprintf( stderr, "Database number selected via -n is out of range\n" );
118                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
119                 exit( 1 );
120         } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
121                 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
122                 exit( 1 );
123         }
124         be = &backends[dbnum];
125
126         /* disable write sync'ing */
127         li = (struct ldbminfo *) be->be_private;
128         li->li_dbcachewsync = 0;
129
130         attr_masks( be->be_private, attr, &indexmask, &syntaxmask );
131         if ( indexmask == 0 ) {
132                 exit( 0 );
133         }
134
135         id = 0;
136         stop = 0;
137         lineno = 0;
138         buf = NULL;
139         lcur = lmax = 0;
140         vals[0] = &bv;
141         vals[1] = NULL;
142         while ( ! stop ) {
143                 char            *type, *val, *s;
144                 int             vlen;
145
146                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
147                         int     len;
148
149                         lineno++;
150                         len = strlen( line );
151                         while ( lcur + len + 1 > lmax ) {
152                                 lmax += BUFSIZ;
153                                 buf = (char *) ch_realloc( buf, lmax );
154                         }
155                         strcpy( buf + lcur, line );
156                         lcur += len;
157                 } else {
158                         stop = 1;
159                 }
160                 if ( line[0] == '\n' || stop && buf && *buf ) {
161                         if ( *buf != '\n' ) {
162                                 if (isdigit(*buf)) {
163                                         id = atol(buf);
164                                 } else {
165                                         id++;
166                                 }
167                                 s = buf;
168                                 elineno = 0;
169                                 while ( (linep = str_getline( &s )) != NULL ) {
170                                         elineno++;
171                                         if ( str_parse_line( linep, &type, &val,
172                                             &vlen ) != 0 ) {
173                                                 Debug( LDAP_DEBUG_PARSE,
174                             "bad line %d in entry ending at line %d ignored\n",
175                                                     elineno, elineno, 0 );
176                                                 continue;
177                                         }
178
179                                         if ( strcasecmp( type, attr ) == 0 ) {
180                                                 bv.bv_val = val;
181                                                 bv.bv_len = vlen;
182                                                 index_add_values( be, attr,
183                                                     vals, id );
184                                         }
185                                 }
186                         }
187                         *buf = '\0';
188                         lcur = 0;
189                 }
190         }
191         (*be->be_close)( be );
192
193         exit( 0 );
194 }