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