]> git.sur5r.net Git - openldap/blob - servers/slapd/slapcommon.c
Fix prev commit
[openldap] / servers / slapd / slapcommon.c
1 /* slapcommon.c - common routine for the slap tools */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7  * Portions Copyright 2003 IBM Corporation.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by Kurt Zeilenga for inclusion
20  * in OpenLDAP Software.  Additional signficant contributors include
21  *    Jong Hyuk Choi
22  *    Hallvard B. Furuseth
23  *    Howard Chu
24  *    Pierangelo Masarati
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/stdlib.h>
32 #include <ac/ctype.h>
33 #include <ac/string.h>
34 #include <ac/socket.h>
35 #include <ac/unistd.h>
36
37 #include "slapcommon.h"
38 #include "lutil.h"
39
40 tool_vars tool_globals;
41
42 #ifdef CSRIMALLOC
43 static char *leakfilename;
44 static FILE *leakfile;
45 #endif
46
47 static void
48 usage( int tool, const char *progname )
49 {
50         char *options = NULL;
51         fprintf( stderr,
52                 "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n"
53                         "\t[-n databasenumber | -b suffix]", progname );
54
55         switch( tool ) {
56         case SLAPADD:
57                 options = "\n\t[-l ldiffile] [-u] [-p [-w] | -r [-i syncreplidlist] [-w]]\n";
58                 break;
59
60         case SLAPCAT:
61                 options = "\t[-l ldiffile] [-m] [-k]\n";
62                 break;
63
64         case SLAPINDEX:
65                 options = "\n";
66                 break;
67         }
68
69         if( options != NULL ) {
70                 fputs( options, stderr );
71         }
72         exit( EXIT_FAILURE );
73 }
74
75
76 /*
77  * slap_tool_init - initialize slap utility, handle program options.
78  * arguments:
79  *      name            program name
80  *      tool            tool code
81  *      argc, argv      command line arguments
82  */
83
84 void
85 slap_tool_init(
86         const char* progname,
87         int tool,
88         int argc, char **argv )
89 {
90         char *options;
91         char *conffile = SLAPD_DEFAULT_CONFIGFILE;
92         struct berval base = { 0, NULL };
93         char *subtree = NULL;
94         char *ldiffile  = NULL;
95         int rc, i, dbnum;
96         int mode = SLAP_TOOL_MODE;
97         int truncatemode = 0;
98
99 #ifdef CSRIMALLOC
100         leakfilename = malloc( strlen( progname ) + sizeof(".leak") );
101         sprintf( leakfilename, "%s.leak", progname );
102         if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
103                 leakfile = stderr;
104         }
105         free( leakfilename );
106 #endif
107
108         switch( tool ) {
109         case SLAPADD:
110                 options = "b:cd:f:i:l:n:prtuvWw";
111                 break;
112
113         case SLAPINDEX:
114                 options = "b:cd:f:n:v";
115                 mode |= SLAP_TOOL_READMAIN;
116                 break;
117
118         case SLAPCAT:
119                 options = "b:cd:f:kl:mn:s:v";
120                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
121                 break;
122
123         default:
124                 fprintf( stderr, "%s: unknown tool mode (%d)\n",
125                          progname, tool );
126                 exit( EXIT_FAILURE );
127         }
128
129         dbnum = -1;
130         while ( (i = getopt( argc, argv, options )) != EOF ) {
131                 switch ( i ) {
132                 case 'b':
133                         base.bv_val = strdup( optarg );
134                         base.bv_len = strlen( base.bv_val );
135                         break;
136
137                 case 'c':       /* enable continue mode */
138                         continuemode++;
139                         break;
140
141                 case 'd':       /* turn on debugging */
142                         ldap_debug += atoi( optarg );
143                         break;
144
145                 case 'f':       /* specify a conf file */
146                         conffile = strdup( optarg );
147                         break;
148
149                 case 'i': /* specify syncrepl id list */
150                         replica_id_string = strdup( optarg );
151                         if ( !isdigit( (unsigned char) *replica_id_string )) {
152                                 usage( tool, progname );
153                                 exit( EXIT_FAILURE );
154                         }
155                         str2clist( &replica_id_strlist, replica_id_string, "," );
156                         for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) ;
157                         replica_id_list = ch_calloc( i + 1, sizeof( int ) );
158                         for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) {
159                                 replica_id_list[i] = atoi( replica_id_strlist[i] );
160                                 if ( replica_id_list[i] >= 1000 ) {
161                                         fprintf(stderr,
162                                                 "%s: syncrepl id %d is out of range [0..999]\n",
163                                                 progname, replica_id_list[i] );
164                                         exit( EXIT_FAILURE );
165                                 }
166                         }
167                         replica_id_list[i] = -1;
168                         break;
169
170                 case 'k':       /* Retrieve sync cookie entry */
171                         retrieve_synccookie = 1;
172                         break;
173
174                 case 'l':       /* LDIF file */
175                         ldiffile = strdup( optarg );
176                         break;
177
178                 case 'm':       /* Retrieve ldapsync entry */
179                         retrieve_ctxcsn = 1;
180                         break;
181
182                 case 'n':       /* which config file db to index */
183                         dbnum = atoi( optarg ) - 1;
184                         break;
185
186                 case 'p':       /* replica promotion */
187                         replica_promotion = 1;          
188                         break;
189
190                 case 'r':       /* replica demotion */
191                         replica_demotion = 1;           
192                         break;
193
194                 case 's':       /* dump subtree */
195                         subtree = strdup( optarg );
196                         break;
197
198                 case 't':       /* turn on truncate */
199                         truncatemode++;
200                         mode |= SLAP_TRUNCATE_MODE;
201                         break;
202
203                 case 'u':       /* dry run */
204                         dryrun++;
205                         break;
206
207                 case 'v':       /* turn on verbose */
208                         verbose++;
209                         break;
210
211                 case 'W':       /* write context csn on every entry add */
212                         update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
213                         /* FIXME : update_ctxcsn = SLAP_TOOL_CTXCSN_ENTRY; */
214                         break;
215
216                 case 'w':       /* write context csn on at the end */
217                         update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
218                         break;
219
220                 default:
221                         usage( tool, progname );
222                         break;
223                 }
224         }
225
226         if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
227                 usage( tool, progname );
228         }
229
230         if ( replica_promotion && replica_demotion ) {
231                 usage( tool, progname );
232         } else if ( !replica_promotion && !replica_demotion ) {
233                 if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
234                         usage( tool, progname );
235                 }
236         }
237
238         if ( ldiffile == NULL ) {
239                 ldiffp = tool == SLAPCAT ? stdout : stdin;
240
241         } else if( (ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
242                 == NULL )
243         {
244                 perror( ldiffile );
245                 exit( EXIT_FAILURE );
246         }
247
248         /*
249          * initialize stuff and figure out which backend we're dealing with
250          */
251
252 #ifdef SLAPD_MODULES
253         if ( module_init() != 0 ) {
254                 fprintf( stderr, "%s: module_init failed!\n", progname );
255                 exit( EXIT_FAILURE );
256         }
257 #endif
258                 
259         rc = slap_init( mode, progname );
260
261         if ( rc != 0 ) {
262                 fprintf( stderr, "%s: slap_init failed!\n", progname );
263                 exit( EXIT_FAILURE );
264         }
265
266         rc = slap_schema_init();
267
268         if ( rc != 0 ) {
269                 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
270                 exit( EXIT_FAILURE );
271         }
272
273         rc = read_config( conffile, 0 );
274
275         if ( rc != 0 ) {
276                 fprintf( stderr, "%s: bad configuration file!\n", progname );
277                 exit( EXIT_FAILURE );
278         }
279
280         ldap_syslog = 0;
281
282         if ( !nbackends ) {
283                 fprintf( stderr, "No databases found in config file\n" );
284                 exit( EXIT_FAILURE );
285         }
286
287         rc = glue_sub_init();
288
289         if ( rc != 0 ) {
290                 fprintf( stderr, "Subordinate configuration error\n" );
291                 exit( EXIT_FAILURE );
292         }
293
294         rc = slap_schema_check();
295
296         if ( rc != 0 ) {
297                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
298                 exit( EXIT_FAILURE );
299         }
300
301         if( subtree ) {
302                 struct berval val;
303                 val.bv_val = subtree;
304                 val.bv_len = strlen( subtree );
305                 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
306                 if( rc != LDAP_SUCCESS ) {
307                         fprintf( stderr, "Invalid subtree DN '%s'\n", optarg );
308                         exit( EXIT_FAILURE );
309                 }
310
311                 if( base.bv_val == NULL && dbnum == -1 )
312                         base = val;
313                 else
314                         free( subtree );
315         }
316
317         if( base.bv_val != NULL ) {
318                 struct berval nbase;
319
320                 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
321                 if( rc != LDAP_SUCCESS ) {
322                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
323                                 progname, base.bv_val );
324                         exit( EXIT_FAILURE );
325                 }
326
327                 be = select_backend( &nbase, 0, 0 );
328                 ber_memfree( nbase.bv_val );
329
330                 if( be == NULL ) {
331                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
332                                 progname, base.bv_val );
333                         exit( EXIT_FAILURE );
334                 }
335                 /* If the named base is a glue master, operate on the
336                  * entire context
337                  */
338                 if (SLAP_GLUE_INSTANCE(be)) {
339                         nosubordinates = 1;
340                 }
341
342         } else if ( dbnum == -1 ) {
343                 if ( nbackends <= 0 ) {
344                         fprintf( stderr, "No available databases\n" );
345                         exit( EXIT_FAILURE );
346                 }
347                 
348                 be = &backends[dbnum=0];
349                 /* If just doing the first by default and it is a
350                  * glue subordinate, find the master.
351                  */
352                 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
353                         if (SLAP_GLUE_SUBORDINATE(be)) {
354                                 nosubordinates = 1;
355                         }
356                         be++;
357                         dbnum++;
358                 }
359
360
361                 if ( dbnum >= nbackends ) {
362                         fprintf( stderr, "Available database(s) "
363                                         "do not allow %s\n", progname );
364                         exit( EXIT_FAILURE );
365                 }
366                 
367                 if ( nosubordinates == 0 && dbnum > 0 ) {
368 #ifdef NEW_LOGGING
369                         LDAP_LOG( BACKEND, ERR, 
370 "The first database does not allow %s; using the first available one (%d)\n",
371                                 progname, dbnum + 1, 0 );
372 #else
373                         Debug( LDAP_DEBUG_ANY,
374 "The first database does not allow %s; using the first available one (%d)\n",
375                                 progname, dbnum + 1, 0 );
376 #endif
377                 }
378
379         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
380                 fprintf( stderr,
381                         "Database number selected via -n is out of range\n"
382                         "Must be in the range 1 to %d"
383                                 " (number of databases in the config file)\n",
384                         nbackends );
385                 exit( EXIT_FAILURE );
386
387         } else {
388                 be = &backends[dbnum];
389         }
390
391 #ifdef CSRIMALLOC
392         mal_leaktrace(1);
393 #endif
394
395         if ( slap_startup( be ) ) {
396                 fprintf( stderr, "slap_startup failed\n" );
397                 exit( EXIT_FAILURE );
398         }
399 }
400
401 void slap_tool_destroy( void )
402 {
403         slap_shutdown( be );
404         slap_destroy();
405 #ifdef SLAPD_MODULES
406         if ( slapMode == SLAP_SERVER_MODE ) {
407         /* always false. just pulls in necessary symbol references. */
408                 lutil_uuidstr(NULL, 0);
409         }
410         module_kill();
411 #endif
412         schema_destroy();
413 #ifdef HAVE_TLS
414         ldap_pvt_tls_destroy();
415 #endif
416         config_destroy();
417
418 #ifdef CSRIMALLOC
419         mal_dumpleaktrace( leakfile );
420 #endif
421 }