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