]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2index.c
Add basic support for MD5 and SHA1 passwords.
[openldap] / servers / slapd / tools / ldif2index.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/socket.h>
5 #include "../slap.h"
6
7 #define DEFAULT_CONFIGFILE      "/usr/local/etc/slapd.conf"
8 #define MAXARGS                 100
9
10 extern void     attr_index_config();
11 extern char     *str_getline();
12 extern char     *attr_normalize();
13 extern int      nbackends;
14 extern Backend  *backends;
15 extern int      ldap_debug;
16
17 int             ldap_debug;
18 int             ldap_syslog;
19 int             ldap_syslog_level;
20 int             global_schemacheck;
21 int             num_entries_sent;
22 int             num_bytes_sent;
23 int             active_threads;
24 char            *default_referral;
25 struct objclass *global_oc;
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
37 static void
38 usage( char *name )
39 {
40         fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-n databasenumber] attr\n", name );
41         exit( 1 );
42 }
43
44 main( int argc, char **argv )
45 {
46         int             i, cargc, indb, stop;
47         char            *cargv[MAXARGS];
48         char            *defargv[MAXARGS];
49         char            *tailorfile, *inputfile;
50         char            *linep, *buf, *attr;
51         char            line[BUFSIZ];
52         int             lineno, elineno;
53         int             lmax, lcur, indexmask, syntaxmask;
54         int             dbnum;
55         unsigned long   id;
56         Backend         *be = NULL;
57         struct berval   bv;
58         struct berval   *vals[2];
59         extern char     *optarg;
60
61         inputfile = NULL;
62         tailorfile = DEFAULT_CONFIGFILE;
63         dbnum = -1;
64         while ( (i = getopt( argc, argv, "d:f:i:n:" )) != EOF ) {
65                 switch ( i ) {
66                 case 'd':       /* turn on debugging */
67                         ldap_debug = atoi( optarg );
68                         break;
69
70                 case 'f':       /* specify a tailor file */
71                         tailorfile = strdup( optarg );
72                         break;
73
74                 case 'i':       /* input file */
75                         inputfile = strdup( optarg );
76                         break;
77
78                 case 'n':       /* which config file db to index */
79                         dbnum = atoi( optarg ) - 1;
80                         break;
81
82                 default:
83                         usage( argv[0] );
84                         break;
85                 }
86         }
87         attr = attr_normalize( argv[argc - 1] );
88         if ( inputfile == NULL ) {
89                 usage( argv[0] );
90         } else {
91                 if ( freopen( inputfile, "r", stdin ) == NULL ) {
92                         perror( inputfile );
93                         exit( 1 );
94                 }
95         }
96
97         init();
98         read_config( tailorfile, &be, NULL );
99
100         if ( dbnum == -1 ) {
101                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
102                         if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
103                             == 0 ) {
104                                 break;
105                         }
106                 }
107                 if ( dbnum == nbackends ) {
108                         fprintf( stderr, "No ldbm database found in config file\n" );
109                         exit( 1 );
110                 }
111         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
112                 fprintf( stderr, "Database number selected via -n is out of range\n" );
113                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
114                 exit( 1 );
115         } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
116                 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
117                 exit( 1 );
118         }
119         be = &backends[dbnum];
120
121         attr_masks( be->be_private, attr, &indexmask, &syntaxmask );
122         if ( indexmask == 0 ) {
123                 exit( 0 );
124         }
125
126         id = 0;
127         stop = 0;
128         lineno = 0;
129         buf = NULL;
130         lcur = lmax = 0;
131         vals[0] = &bv;
132         vals[1] = NULL;
133         while ( ! stop ) {
134                 char            *type, *val, *s;
135                 int             vlen;
136
137                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
138                         int     len;
139
140                         lineno++;
141                         len = strlen( line );
142                         while ( lcur + len + 1 > lmax ) {
143                                 lmax += BUFSIZ;
144                                 buf = (char *) ch_realloc( buf, lmax );
145                         }
146                         strcpy( buf + lcur, line );
147                         lcur += len;
148                 } else {
149                         stop = 1;
150                 }
151                 if ( line[0] == '\n' || stop && buf && *buf ) {
152                         if ( *buf != '\n' ) {
153                                 id++;
154                                 s = buf;
155                                 elineno = 0;
156                                 while ( (linep = str_getline( &s )) != NULL ) {
157                                         elineno++;
158                                         if ( str_parse_line( linep, &type, &val,
159                                             &vlen ) != 0 ) {
160                                                 Debug( LDAP_DEBUG_PARSE,
161                             "bad line %d in entry ending at line %d ignored\n",
162                                                     elineno, elineno, 0 );
163                                                 continue;
164                                         }
165
166                                         if ( strcasecmp( type, attr ) == 0 ) {
167                                                 bv.bv_val = val;
168                                                 bv.bv_len = vlen;
169                                                 index_add_values( be, attr,
170                                                     vals, id );
171                                         }
172                                 }
173                         }
174                         *buf = '\0';
175                         lcur = 0;
176                 }
177         }
178         (*be->be_close)( be );
179
180         exit( 0 );
181 }