]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2id2entry.c
Import slapd changes from devel including:
[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 static char     *tailorfile;
18 static char     *inputfile;
19  
20 static void
21 usage( char *name )
22 {
23         fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-n databasenumber]\n", name );
24         exit( 1 );
25 }
26
27 int
28 main( int argc, char **argv )
29 {
30         int             i, cargc, indb, stop, status;
31         char            *cargv[MAXARGS];
32         char            *defargv[MAXARGS];
33         char            *linep, *buf;
34         char            line[BUFSIZ], idbuf[BUFSIZ];
35         int             lmax, lcur;
36         int             dbnum;
37         ID              id;
38         struct dbcache  *db;
39         Backend         *be = NULL;
40         struct ldbminfo *li;
41         struct berval   bv;
42         struct berval   *vals[2];
43         Avlnode         *avltypes = NULL;
44         FILE            *fp;
45
46         tailorfile = SLAPD_DEFAULT_CONFIGFILE;
47         dbnum = -1;
48         while ( (i = getopt( argc, argv, "d:f:i:n:" )) != EOF ) {
49                 switch ( i ) {
50                 case 'd':       /* turn on debugging */
51                         ldap_debug = atoi( optarg );
52                         break;
53
54                 case 'f':       /* specify a tailor file */
55                         tailorfile = strdup( optarg );
56                         break;
57
58                 case 'i':       /* input file */
59                         inputfile = strdup( optarg );
60                         break;
61
62                 case 'n':       /* which config file db to index */
63                         dbnum = atoi( optarg ) - 1;
64                         break;
65
66                 default:
67                         usage( argv[0] );
68                         break;
69                 }
70         }
71         if ( inputfile == NULL ) {
72                 usage( argv[0] );
73         } else {
74                 if ( freopen( inputfile, "r", stdin ) == NULL ) {
75                         perror( inputfile );
76                         exit( 1 );
77                 }
78         }
79
80         /*
81          * initialize stuff and figure out which backend we're dealing with
82          */
83
84         init();
85         read_config( tailorfile, &be, NULL );
86
87         if ( dbnum == -1 ) {
88                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
89                         if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
90                             == 0 ) {
91                                 break;
92                         }
93                 }
94                 if ( dbnum == nbackends ) {
95                         fprintf( stderr, "No ldbm database found in config file\n" );
96                         exit( 1 );
97                 }
98         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
99                 fprintf( stderr, "Database number selected via -n is out of range\n" );
100                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
101                 exit( 1 );
102         } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
103                 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
104                 exit( 1 );
105         }
106         be = &backends[dbnum];
107
108         /* disable write sync'ing */
109         li = (struct ldbminfo *) be->be_private;
110         li->li_dbcachewsync = 0;
111
112         if ( (db = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, LDBM_NEWDB ))
113             == NULL ) {
114                 perror( "id2entry file" );
115                 exit( 1 );
116         }
117
118         id = 0;
119         stop = 0;
120         buf = NULL;
121         lcur = lmax = 0;
122         vals[0] = &bv;
123         vals[1] = NULL;
124         while ( ! stop ) {
125                 char            *type, *val, *s;
126                 int             vlen;
127                 Datum           key, data;
128
129                 ldbm_datum_init( key );
130                 ldbm_datum_init( data );
131
132                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
133                         int     len, idlen;
134
135                         len = strlen( line );
136                         if ( buf == NULL || *buf == '\0' ) {
137                                 if (!isdigit(line[0])) {
138                                         sprintf( idbuf, "%d\n", id + 1 );
139                                         idlen = strlen( idbuf );
140                                 } else {
141                                         id = atol(line) - 1;
142                                         idlen = 0;
143                                 }
144                         } else {
145                                 idlen = 0;
146                         }
147
148                         while ( lcur + len + idlen + 1 > lmax ) {
149                                 lmax += BUFSIZ;
150                                 buf = (char *) ch_realloc( buf, lmax );
151                         }
152
153                         if ( idlen > 0 ) {
154                                 strcpy( buf + lcur, idbuf );
155                                 lcur += idlen;
156                         }
157                         strcpy( buf + lcur, line );
158                         lcur += len;
159                 } else {
160                         stop = 1;
161                 }
162                 if ( line[0] == '\n' || stop && buf && *buf ) {
163                         if ( *buf != '\n' ) {
164                                 int len;
165
166                                 id++;
167                                 key.dptr = (char *) &id;
168                                 key.dsize = sizeof(ID);
169                                 data.dptr = buf;
170                                 len = strlen(buf);
171                                 if (buf[len - 1] == '\n')
172                                         buf[--len] = '\0';
173                                 data.dsize = len + 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 }