]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2id2entry.c
Import KRB_LIBS fix from -devel.
[openldap] / servers / slapd / tools / ldif2id2entry.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/string.h>
6 #include <ac/socket.h>
7 #include <ac/unistd.h>
8
9 #include "ldapconfig.h"
10 #include "../slap.h"
11 #include "../back-ldbm/back-ldbm.h"
12
13 #define MAXARGS                 100
14
15 int             ldap_debug;
16 int             ldap_syslog;
17 int             ldap_syslog_level;
18 int             global_schemacheck;
19 long            num_entries_sent;
20 long            num_bytes_sent;
21 int             active_threads;
22 char            *default_referral;
23 struct objclass *global_oc;
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
35 static char     *tailorfile;
36 static char     *inputfile;
37  
38 static void
39 usage( char *name )
40 {
41         fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-n databasenumber]\n", name );
42         exit( 1 );
43 }
44
45 int
46 main( int argc, char **argv )
47 {
48         int             i, cargc, indb, stop, status;
49         char            *cargv[MAXARGS];
50         char            *defargv[MAXARGS];
51         char            *linep, *buf;
52         char            line[BUFSIZ], idbuf[BUFSIZ];
53         int             lmax, lcur;
54         int             dbnum;
55         ID              id;
56         struct dbcache  *db;
57         Backend         *be;
58         struct berval   bv;
59         struct berval   *vals[2];
60         Avlnode         *avltypes = NULL;
61         FILE            *fp;
62
63         tailorfile = SLAPD_DEFAULT_CONFIGFILE;
64         dbnum = -1;
65         while ( (i = getopt( argc, argv, "d:f:i:n:" )) != EOF ) {
66                 switch ( i ) {
67                 case 'd':       /* turn on debugging */
68                         ldap_debug = atoi( optarg );
69                         break;
70
71                 case 'f':       /* specify a tailor file */
72                         tailorfile = strdup( optarg );
73                         break;
74
75                 case 'i':       /* input file */
76                         inputfile = strdup( optarg );
77                         break;
78
79                 case 'n':       /* which config file db to index */
80                         dbnum = atoi( optarg ) - 1;
81                         break;
82
83                 default:
84                         usage( argv[0] );
85                         break;
86                 }
87         }
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         /*
98          * initialize stuff and figure out which backend we're dealing with
99          */
100
101         init();
102         read_config( tailorfile, &be, NULL );
103
104         if ( dbnum == -1 ) {
105                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
106                         if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
107                             == 0 ) {
108                                 break;
109                         }
110                 }
111                 if ( dbnum == nbackends ) {
112                         fprintf( stderr, "No ldbm database found in config file\n" );
113                         exit( 1 );
114                 }
115         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
116                 fprintf( stderr, "Database number selected via -n is out of range\n" );
117                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
118                 exit( 1 );
119         } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
120                 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
121                 exit( 1 );
122         }
123         be = &backends[dbnum];
124
125         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_NEWDB ))
126             == NULL ) {
127                 perror( "id2entry file" );
128                 exit( 1 );
129         }
130
131         id = 0;
132         stop = 0;
133         buf = NULL;
134         lcur = lmax = 0;
135         vals[0] = &bv;
136         vals[1] = NULL;
137         while ( ! stop ) {
138                 char            *type, *val, *s;
139                 int             vlen;
140                 Datum           key, data;
141
142                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
143                         int     len, idlen;
144
145                         len = strlen( line );
146                         if ( buf == NULL || *buf == '\0' ) {
147                                 sprintf( idbuf, "%lu\n", id + 1 );
148                                 idlen = strlen( idbuf );
149                         } else {
150                                 idlen = 0;
151                         }
152
153                         while ( lcur + len + idlen + 1 > lmax ) {
154                                 lmax += BUFSIZ;
155                                 buf = (char *) ch_realloc( buf, lmax );
156                         }
157
158                         if ( idlen > 0 ) {
159                                 strcpy( buf + lcur, idbuf );
160                                 lcur += idlen;
161                         }
162                         strcpy( buf + lcur, line );
163                         lcur += len;
164                 } else {
165                         stop = 1;
166                 }
167                 if ( line[0] == '\n' || stop && buf && *buf ) {
168                         if ( *buf != '\n' ) {
169                                 id++;
170                                 key.dptr = (char *) &id;
171                                 key.dsize = sizeof(ID);
172                                 data.dptr = buf;
173                                 data.dsize = strlen( buf ) + 1;
174                                 if ( ldbm_store( db->dbc_db, key, data,
175                                     LDBM_INSERT ) != 0 ) {
176                                         fputs("id2entry ldbm_store failed\n",
177                                               stderr);
178                                         exit( 1 );
179                                 }
180                         }
181                         *buf = '\0';
182                         lcur = 0;
183                         line[0] = '\0';
184                 }
185         }
186         (*be->be_close)( be );
187
188         id++;
189         sprintf( line, "%s/NEXTID",
190             ((struct ldbminfo *) be->be_private)->li_directory );
191         if ( (fp = fopen( line, "w" )) == NULL ) {
192                 perror( line );
193                 fprintf( stderr, "Could not write next id %ld\n", id );
194         } else {
195                 fprintf( fp, "%ld\n", id );
196                 fclose( fp );
197         }
198
199         exit( 0 );
200 }