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