]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/slapcommon.c
First round of imports from HEAD
[openldap] / servers / slapd / tools / slapcommon.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /* slapcommon.c - common routine for the slap tools */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/stdlib.h>
13 #include <ac/ctype.h>
14 #include <ac/string.h>
15 #include <ac/socket.h>
16 #include <ac/unistd.h>
17
18 #include "slapcommon.h"
19 #include "lutil.h"
20
21
22 char    *progname       = NULL;
23 char    *conffile       = SLAPD_DEFAULT_CONFIGFILE;
24 int             truncatemode = 0;
25 int             verbose         = 0;
26 int             continuemode = 0;
27 int             nosubordinates = 0;
28 int             dryrun = 0;
29
30 char    *ldiffile       = NULL;
31 FILE    *ldiffp         = NULL;
32
33 #ifdef CSRIMALLOC
34         char *leakfilename;
35         FILE *leakfile;
36 #endif
37
38 Backend *be = NULL;
39
40 static void
41 usage( int tool )
42 {
43         char *options = NULL;
44         fprintf( stderr,
45                 "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n"
46                         "\t[-n databasenumber | -b suffix]", progname );
47
48         switch( tool ) {
49         case SLAPADD:
50                 options = "\t[-l ldiffile]\n";
51                 break;
52
53         case SLAPCAT:
54                 options = "\t[-l ldiffile]\n";
55                 break;
56
57         case SLAPINDEX:
58                 options = "\n";
59                 break;
60         }
61
62         if( options != NULL ) {
63                 fputs( options, stderr );
64         }
65         exit( EXIT_FAILURE );
66 }
67
68
69 /*
70  * slap_tool_init - initialize slap utility, handle program options.
71  * arguments:
72  *      name            program name
73  *      tool            tool code
74  *      argc, argv      command line arguments
75  */
76
77 void
78 slap_tool_init(
79         const char* name,
80         int tool,
81         int argc, char **argv )
82 {
83         char *options;
84         struct berval base = { 0, NULL };
85         int rc, i, dbnum;
86         int mode = SLAP_TOOL_MODE;
87
88         progname = lutil_progname( name, argc, argv );
89
90 #ifdef CSRIMALLOC
91         leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
92         sprintf( leakfilename, "%s.leak", progname );
93         if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
94                 leakfile = stderr;
95         }
96         free( leakfilename );
97 #endif
98
99         switch( tool ) {
100         case SLAPADD:
101                 options = "b:cd:f:l:n:tuv";
102                 break;
103
104         case SLAPINDEX:
105                 options = "b:cd:f:n:v";
106                 break;
107
108         case SLAPCAT:
109                 options = "b:cd:f:l:n:v";
110                 break;
111
112         default:
113                 fprintf( stderr, "%s: unknown tool mode (%d)\n",
114                          progname, tool );
115                 exit( EXIT_FAILURE );
116         }
117
118         ldiffile = NULL;
119         conffile = SLAPD_DEFAULT_CONFIGFILE;
120         dbnum = -1;
121         while ( (i = getopt( argc, argv, options )) != EOF ) {
122                 switch ( i ) {
123                 case 'b':
124                         base.bv_val = strdup( optarg );
125                         base.bv_len = strlen( base.bv_val );
126                         break;
127
128                 case 'c':       /* enable continue mode */
129                         continuemode++;
130                         break;
131
132                 case 'd':       /* turn on debugging */
133                         ldap_debug += atoi( optarg );
134                         break;
135
136                 case 'f':       /* specify a conf file */
137                         conffile = strdup( optarg );
138                         break;
139
140                 case 'l':       /* LDIF file */
141                         ldiffile = strdup( optarg );
142                         break;
143
144                 case 'n':       /* which config file db to index */
145                         dbnum = atoi( optarg ) - 1;
146                         break;
147
148                 case 't':       /* turn on truncate */
149                         truncatemode++;
150                         mode |= SLAP_TRUNCATE_MODE;
151                         break;
152
153                 case 'u':       /* dry run */
154                         dryrun++;
155                         break;
156
157                 case 'v':       /* turn on verbose */
158                         verbose++;
159                         break;
160
161                 default:
162                         usage( tool );
163                         break;
164                 }
165         }
166
167         if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
168                 usage( tool );
169         }
170
171         if ( ldiffile == NULL ) {
172                 ldiffp = tool == SLAPCAT ? stdout : stdin;
173
174         } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
175                 == NULL )
176         {
177                 perror( ldiffile );
178                 exit( EXIT_FAILURE );
179         }
180
181         /*
182          * initialize stuff and figure out which backend we're dealing with
183          */
184
185 #ifdef SLAPD_MODULES
186         if ( module_init() != 0 ) {
187                 fprintf( stderr, "%s: module_init failed!\n", progname );
188                 exit( EXIT_FAILURE );
189         }
190 #endif
191                 
192         rc = slap_init( mode, progname );
193
194         if ( rc != 0 ) {
195                 fprintf( stderr, "%s: slap_init failed!\n", progname );
196                 exit( EXIT_FAILURE );
197         }
198
199         rc = slap_schema_init();
200
201         if ( rc != 0 ) {
202                 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
203                 exit( EXIT_FAILURE );
204         }
205
206         rc = read_config( conffile, 0 );
207
208         if ( rc != 0 ) {
209                 fprintf( stderr, "%s: bad configuration file!\n", progname );
210                 exit( EXIT_FAILURE );
211         }
212
213         if ( !nbackends ) {
214                 fprintf( stderr, "No databases found in config file\n" );
215                 exit( EXIT_FAILURE );
216         }
217
218         rc = glue_sub_init();
219
220         if ( rc != 0 ) {
221                 fprintf( stderr, "Subordinate configuration error\n" );
222                 exit( EXIT_FAILURE );
223         }
224
225         rc = slap_schema_check();
226
227         if ( rc != 0 ) {
228                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
229                 exit( EXIT_FAILURE );
230         }
231
232         if( base.bv_val != NULL ) {
233                 struct berval *nbase = NULL;
234
235                 rc = dnNormalize( NULL, &base, &nbase );
236                 if( rc != LDAP_SUCCESS ) {
237                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
238                                 progname, base.bv_val );
239                         exit( EXIT_FAILURE );
240                 }
241
242                 be = select_backend( nbase, 0, 0 );
243                 ber_bvfree( nbase );
244
245                 if( be == NULL ) {
246                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
247                                 progname, base.bv_val );
248                         exit( EXIT_FAILURE );
249                 }
250                 /* If the named base is a glue master, operate on the
251                  * entire context
252                  */
253                 if (be->be_flags & SLAP_BFLAG_GLUE_INSTANCE)
254                         nosubordinates = 1;
255
256         } else if ( dbnum == -1 ) {
257                 be = &backends[dbnum=0];
258                 /* If just doing the first by default and it is a
259                  * glue subordinate, find the master.
260                  */
261                 while (be->be_flags & SLAP_BFLAG_GLUE_SUBORDINATE) {
262                         nosubordinates = 1;
263                         be++;
264                 }
265
266         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
267                 fprintf( stderr,
268                         "Database number selected via -n is out of range\n"
269                         "Must be in the range 1 to %d"
270                                 " (number of databases in the config file)\n",
271                         nbackends );
272                 exit( EXIT_FAILURE );
273
274         } else {
275                 be = &backends[dbnum];
276         }
277
278 #ifdef CSRIMALLOC
279         mal_leaktrace(1);
280 #endif
281
282         slap_startup( be );
283 }
284
285 void slap_tool_destroy( void )
286 {
287         slap_shutdown( be );
288         slap_destroy();
289 #ifdef SLAPD_MODULES
290         if ( slapMode == SLAP_SERVER_MODE ) {
291         /* always false. just pulls in necessary symbol references. */
292                 lutil_uuidstr(NULL, 0);
293         }
294         module_kill();
295 #endif
296         schema_destroy();
297 #ifdef HAVE_TLS
298         ldap_pvt_tls_destroy();
299 #endif
300         config_destroy();
301
302 #ifdef CSRIMALLOC
303         mal_dumpleaktrace( leakfile );
304 #endif
305 }