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