]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2id2entry.c
Rework ac/socket.h for HAVE_WINSOCK:
[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, stop;
31         char            *buf;
32         char            line[BUFSIZ], idbuf[BUFSIZ];
33         int             lmax, lcur;
34         int             dbnum;
35         ID              id;
36         struct dbcache  *db;
37         Backend         *be = NULL;
38         struct ldbminfo *li;
39         struct berval   bv;
40         struct berval   *vals[2];
41         FILE            *fp;
42
43         tailorfile = SLAPD_DEFAULT_CONFIGFILE;
44         dbnum = -1;
45         while ( (i = getopt( argc, argv, "d:f:i:n:" )) != EOF ) {
46                 switch ( i ) {
47                 case 'd':       /* turn on debugging */
48                         ldap_debug = atoi( optarg );
49                         break;
50
51                 case 'f':       /* specify a tailor file */
52                         tailorfile = strdup( optarg );
53                         break;
54
55                 case 'i':       /* input file */
56                         inputfile = strdup( optarg );
57                         break;
58
59                 case 'n':       /* which config file db to index */
60                         dbnum = atoi( optarg ) - 1;
61                         break;
62
63                 default:
64                         usage( argv[0] );
65                         break;
66                 }
67         }
68         if ( inputfile == NULL ) {
69                 usage( argv[0] );
70         } else {
71                 if ( freopen( inputfile, "r", stdin ) == NULL ) {
72                         perror( inputfile );
73                         exit( 1 );
74                 }
75         }
76
77         /*
78          * initialize stuff and figure out which backend we're dealing with
79          */
80
81         slap_init(SLAP_TOOL_MODE, "ldif2id2entry");
82         read_config( tailorfile );
83
84         if ( dbnum == -1 ) {
85                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
86                         if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
87                             == 0 ) {
88                                 break;
89                         }
90                 }
91                 if ( dbnum == nbackends ) {
92                         fprintf( stderr, "No ldbm database found in config file\n" );
93                         exit( 1 );
94                 }
95         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
96                 fprintf( stderr, "Database number selected via -n is out of range\n" );
97                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
98                 exit( 1 );
99         } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
100                 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
101                 exit( 1 );
102         }
103
104         slap_startup(dbnum);
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                 Datum           key, data;
126
127                 ldbm_datum_init( key );
128                 ldbm_datum_init( data );
129
130                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
131                         int     len, idlen;
132
133                         len = strlen( line );
134                         if ( buf == NULL || *buf == '\0' ) {
135                                 if (!isdigit((unsigned char) line[0])) {
136                                         sprintf( idbuf, "%ld\n", id + 1 );
137                                         idlen = strlen( idbuf );
138                                 } else {
139                                         id = atol(line) - 1;
140                                         idlen = 0;
141                                 }
142                         } else {
143                                 idlen = 0;
144                         }
145
146                         while ( lcur + len + idlen + 1 > lmax ) {
147                                 lmax += BUFSIZ;
148                                 buf = (char *) ch_realloc( buf, lmax );
149                         }
150
151                         if ( idlen > 0 ) {
152                                 strcpy( buf + lcur, idbuf );
153                                 lcur += idlen;
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                                 int len;
163
164                                 id++;
165                                 key.dptr = (char *) &id;
166                                 key.dsize = sizeof(ID);
167                                 data.dptr = buf;
168                                 len = strlen(buf);
169                                 if (buf[len - 1] == '\n')
170                                         buf[--len] = '\0';
171                                 data.dsize = len + 1;
172                                 if ( ldbm_store( db->dbc_db, key, data,
173                                     LDBM_INSERT ) != 0 ) {
174                                         fputs("id2entry ldbm_store failed\n",
175                                               stderr);
176                                         exit( 1 );
177                                 }
178                         }
179                         *buf = '\0';
180                         lcur = 0;
181                         line[0] = '\0';
182                 }
183         }
184
185         slap_shutdown(dbnum);
186
187         id++;
188         sprintf( line, "%s/NEXTID",
189             ((struct ldbminfo *) be->be_private)->li_directory );
190         if ( (fp = fopen( line, "w" )) == NULL ) {
191                 perror( line );
192                 fprintf( stderr, "Could not write next id %ld\n", id );
193         } else {
194                 fprintf( fp, "%ld\n", id );
195                 fclose( fp );
196         }
197
198         slap_destroy();
199
200         exit( 0 );
201 }