]> git.sur5r.net Git - openldap/blob - servers/slapd/slapcommon.c
allow '-o value-check={yes|no}' for optional value checking, disabled in quick 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-2010 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 #include "ldif.h"
40
41 tool_vars tool_globals;
42
43 #ifdef CSRIMALLOC
44 static char *leakfilename;
45 static FILE *leakfile;
46 #endif
47
48 static LDIFFP dummy;
49
50 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
51 int start_syslog;
52 static char **syslog_unknowns;
53 #ifdef LOG_LOCAL4
54 static int syslogUser = SLAP_DEFAULT_SYSLOG_USER;
55 #endif /* LOG_LOCAL4 */
56 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
57
58 static void
59 usage( int tool, const char *progname )
60 {
61         char *options = NULL;
62         fprintf( stderr,
63                 "usage: %s [-v] [-d debuglevel] [-f configfile] [-F configdir] [-o <name>[=<value>]]",
64                 progname );
65
66         switch( tool ) {
67         case SLAPACL:
68                 options = "\n\t[-U authcID | -D authcDN] [-X authzID | -o authzDN=<DN>]"
69                         "\n\t-b DN [-u] [attr[/access][:value]] [...]\n";
70                 break;
71
72         case SLAPADD:
73                 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]\n"
74                         "\t[-l ldiffile] [-j linenumber] [-q] [-u] [-s] [-w]\n";
75                 break;
76
77         case SLAPAUTH:
78                 options = "\n\t[-U authcID] [-X authzID] [-R realm] [-M mech] ID [...]\n";
79                 break;
80
81         case SLAPCAT:
82                 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]"
83                         " [-l ldiffile] [-a filter] [-s subtree] [-H url]\n";
84                 break;
85
86         case SLAPDN:
87                 options = "\n\t[-N | -P] DN [...]\n";
88                 break;
89
90         case SLAPINDEX:
91                 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix] [attr ...] [-q] [-t]\n";
92                 break;
93
94         case SLAPTEST:
95                 options = " [-n databasenumber] [-u]\n";
96                 break;
97
98         case SLAPSCHEMA:
99                 options = " [-c]\n\t[-g] [-n databasenumber | -b suffix]"
100                         " [-l errorfile] [-a filter] [-s subtree] [-H url]\n";
101                 break;
102         }
103
104         if ( options != NULL ) {
105                 fputs( options, stderr );
106         }
107         exit( EXIT_FAILURE );
108 }
109
110 static int
111 parse_slapopt( int tool, int *mode )
112 {
113         size_t  len = 0;
114         char    *p;
115
116         p = strchr( optarg, '=' );
117         if ( p != NULL ) {
118                 len = p - optarg;
119                 p++;
120         }
121
122         if ( strncasecmp( optarg, "sockurl", len ) == 0 ) {
123                 if ( !BER_BVISNULL( &listener_url ) ) {
124                         ber_memfree( listener_url.bv_val );
125                 }
126                 ber_str2bv( p, 0, 1, &listener_url );
127
128         } else if ( strncasecmp( optarg, "domain", len ) == 0 ) {
129                 if ( !BER_BVISNULL( &peer_domain ) ) {
130                         ber_memfree( peer_domain.bv_val );
131                 }
132                 ber_str2bv( p, 0, 1, &peer_domain );
133
134         } else if ( strncasecmp( optarg, "peername", len ) == 0 ) {
135                 if ( !BER_BVISNULL( &peer_name ) ) {
136                         ber_memfree( peer_name.bv_val );
137                 }
138                 ber_str2bv( p, 0, 1, &peer_name );
139
140         } else if ( strncasecmp( optarg, "sockname", len ) == 0 ) {
141                 if ( !BER_BVISNULL( &sock_name ) ) {
142                         ber_memfree( sock_name.bv_val );
143                 }
144                 ber_str2bv( p, 0, 1, &sock_name );
145
146         } else if ( strncasecmp( optarg, "ssf", len ) == 0 ) {
147                 if ( lutil_atou( &ssf, p ) ) {
148                         Debug( LDAP_DEBUG_ANY, "unable to parse ssf=\"%s\".\n", p, 0, 0 );
149                         return -1;
150                 }
151
152         } else if ( strncasecmp( optarg, "transport_ssf", len ) == 0 ) {
153                 if ( lutil_atou( &transport_ssf, p ) ) {
154                         Debug( LDAP_DEBUG_ANY, "unable to parse transport_ssf=\"%s\".\n", p, 0, 0 );
155                         return -1;
156                 }
157
158         } else if ( strncasecmp( optarg, "tls_ssf", len ) == 0 ) {
159                 if ( lutil_atou( &tls_ssf, p ) ) {
160                         Debug( LDAP_DEBUG_ANY, "unable to parse tls_ssf=\"%s\".\n", p, 0, 0 );
161                         return -1;
162                 }
163
164         } else if ( strncasecmp( optarg, "sasl_ssf", len ) == 0 ) {
165                 if ( lutil_atou( &sasl_ssf, p ) ) {
166                         Debug( LDAP_DEBUG_ANY, "unable to parse sasl_ssf=\"%s\".\n", p, 0, 0 );
167                         return -1;
168                 }
169
170         } else if ( strncasecmp( optarg, "authzDN", len ) == 0 ) {
171                 ber_str2bv( p, 0, 1, &authzDN );
172
173 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
174         } else if ( strncasecmp( optarg, "syslog", len ) == 0 ) {
175                 if ( parse_debug_level( p, &ldap_syslog, &syslog_unknowns ) ) {
176                         return -1;
177                 }
178                 start_syslog = 1;
179
180         } else if ( strncasecmp( optarg, "syslog-level", len ) == 0 ) {
181                 if ( parse_syslog_level( p, &ldap_syslog_level ) ) {
182                         return -1;
183                 }
184                 start_syslog = 1;
185
186 #ifdef LOG_LOCAL4
187         } else if ( strncasecmp( optarg, "syslog-user", len ) == 0 ) {
188                 if ( parse_syslog_user( p, &syslogUser ) ) {
189                         return -1;
190                 }
191                 start_syslog = 1;
192 #endif /* LOG_LOCAL4 */
193 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
194
195         } else if ( strncasecmp( optarg, "schema-check", len ) == 0 ) {
196                 switch ( tool ) {
197                 case SLAPADD:
198                         if ( strcasecmp( p, "yes" ) == 0 ) {
199                                 *mode &= ~SLAP_TOOL_NO_SCHEMA_CHECK;
200                         } else if ( strcasecmp( p, "no" ) == 0 ) {
201                                 *mode |= SLAP_TOOL_NO_SCHEMA_CHECK;
202                         } else {
203                                 Debug( LDAP_DEBUG_ANY, "unable to parse schema-check=\"%s\".\n", p, 0, 0 );
204                                 return -1;
205                         }
206                         break;
207
208                 default:
209                         Debug( LDAP_DEBUG_ANY, "schema-check meaningless for tool.\n", 0, 0, 0 );
210                         break;
211                 }
212
213         } else if ( strncasecmp( optarg, "value-check", len ) == 0 ) {
214                 switch ( tool ) {
215                 case SLAPADD:
216                         if ( strcasecmp( p, "yes" ) == 0 ) {
217                                 *mode |= SLAP_TOOL_VALUE_CHECK;
218                         } else if ( strcasecmp( p, "no" ) == 0 ) {
219                                 *mode &= ~SLAP_TOOL_VALUE_CHECK;
220                         } else {
221                                 Debug( LDAP_DEBUG_ANY, "unable to parse value-check=\"%s\".\n", p, 0, 0 );
222                                 return -1;
223                         }
224                         break;
225
226                 default:
227                         Debug( LDAP_DEBUG_ANY, "value-check meaningless for tool.\n", 0, 0, 0 );
228                         break;
229                 }
230
231         } else {
232                 return -1;
233         }
234
235         return 0;
236 }
237
238 /*
239  * slap_tool_init - initialize slap utility, handle program options.
240  * arguments:
241  *      name            program name
242  *      tool            tool code
243  *      argc, argv      command line arguments
244  */
245
246 static int need_shutdown;
247
248 void
249 slap_tool_init(
250         const char* progname,
251         int tool,
252         int argc, char **argv )
253 {
254         char *options;
255         char *conffile = NULL;
256         char *confdir = NULL;
257         struct berval base = BER_BVNULL;
258         char *filterstr = NULL;
259         char *subtree = NULL;
260         char *ldiffile  = NULL;
261         char **debug_unknowns = NULL;
262         int rc, i;
263         int mode = SLAP_TOOL_MODE;
264         int truncatemode = 0;
265         int use_glue = 1;
266         int writer;
267
268 #ifdef LDAP_DEBUG
269         /* tools default to "none", so that at least LDAP_DEBUG_ANY 
270          * messages show up; use -d 0 to reset */
271         slap_debug = LDAP_DEBUG_NONE;
272         ldif_debug = slap_debug;
273 #endif
274         ldap_syslog = 0;
275
276 #ifdef CSRIMALLOC
277         leakfilename = malloc( strlen( progname ) + STRLENOF( ".leak" ) + 1 );
278         sprintf( leakfilename, "%s.leak", progname );
279         if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
280                 leakfile = stderr;
281         }
282         free( leakfilename );
283         leakfilename = NULL;
284 #endif
285
286         scope = LDAP_SCOPE_DEFAULT;
287
288         switch( tool ) {
289         case SLAPADD:
290                 options = "b:cd:f:F:gj:l:n:o:qsS:uvw";
291                 break;
292
293         case SLAPCAT:
294                 options = "a:b:cd:f:F:gH:l:n:o:s:v";
295                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
296                 break;
297
298         case SLAPDN:
299                 options = "d:f:F:No:Pv";
300                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
301                 break;
302
303         case SLAPMODIFY:
304                 options = "b:cd:f:F:gj:l:n:o:qsS:uvw";
305                 break;
306
307         case SLAPSCHEMA:
308                 options = "a:b:cd:f:F:gH:l:n:o:s:v";
309                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
310                 break;
311
312         case SLAPTEST:
313                 options = "d:f:F:n:o:Quv";
314                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
315                 break;
316
317         case SLAPAUTH:
318                 options = "d:f:F:M:o:R:U:vX:";
319                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
320                 break;
321
322         case SLAPINDEX:
323                 options = "b:cd:f:F:gn:o:qtv";
324                 mode |= SLAP_TOOL_READMAIN;
325                 break;
326
327         case SLAPACL:
328                 options = "b:D:d:f:F:o:uU:vX:";
329                 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
330                 break;
331
332         default:
333                 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
334                 exit( EXIT_FAILURE );
335         }
336
337         dbnum = -1;
338         while ( (i = getopt( argc, argv, options )) != EOF ) {
339                 switch ( i ) {
340                 case 'a':
341                         filterstr = ch_strdup( optarg );
342                         break;
343
344                 case 'b':
345                         ber_str2bv( optarg, 0, 1, &base );
346                         break;
347
348                 case 'c':       /* enable continue mode */
349                         continuemode++;
350                         break;
351
352                 case 'd': {     /* turn on debugging */
353                         int     level = 0;
354
355                         if ( parse_debug_level( optarg, &level, &debug_unknowns ) ) {
356                                 usage( tool, progname );
357                         }
358 #ifdef LDAP_DEBUG
359                         if ( level == 0 ) {
360                                 /* allow to reset log level */
361                                 slap_debug = 0;
362
363                         } else {
364                                 slap_debug |= level;
365                         }
366 #else
367                         if ( level != 0 )
368                                 fputs( "must compile with LDAP_DEBUG for debugging\n",
369                                        stderr );
370 #endif
371                         } break;
372
373                 case 'D':
374                         ber_str2bv( optarg, 0, 1, &authcDN );
375                         break;
376
377                 case 'f':       /* specify a conf file */
378                         conffile = ch_strdup( optarg );
379                         break;
380
381                 case 'F':       /* specify a conf dir */
382                         confdir = ch_strdup( optarg );
383                         break;
384
385                 case 'g':       /* disable subordinate glue */
386                         use_glue = 0;
387                         break;
388
389                 case 'H': {
390                         LDAPURLDesc *ludp;
391                         int rc;
392
393                         rc = ldap_url_parse_ext( optarg, &ludp,
394                                 LDAP_PVT_URL_PARSE_NOEMPTY_HOST | LDAP_PVT_URL_PARSE_NOEMPTY_DN );
395                         if ( rc != LDAP_URL_SUCCESS ) {
396                                 usage( tool, progname );
397                         }
398
399                         /* don't accept host, port, attrs, extensions */
400                         if ( ldap_pvt_url_scheme2proto( ludp->lud_scheme ) != LDAP_PROTO_TCP ) {
401                                 usage( tool, progname );
402                         }
403
404                         if ( ludp->lud_host != NULL ) {
405                                 usage( tool, progname );
406                         }
407
408                         if ( ludp->lud_port != 0 ) {
409                                 usage( tool, progname );
410                         }
411
412                         if ( ludp->lud_attrs != NULL ) {
413                                 usage( tool, progname );
414                         }
415
416                         if ( ludp->lud_exts != NULL ) {
417                                 usage( tool, progname );
418                         }
419
420                         if ( ludp->lud_dn != NULL && ludp->lud_dn[0] != '\0' ) {
421                                 subtree = ludp->lud_dn;
422                                 ludp->lud_dn = NULL;
423                         }
424
425                         if ( ludp->lud_filter != NULL && ludp->lud_filter[0] != '\0' ) {
426                                 filterstr = ludp->lud_filter;
427                                 ludp->lud_filter = NULL;
428                         }
429
430                         scope = ludp->lud_scope;
431
432                         ldap_free_urldesc( ludp );
433                         } break;
434
435                 case 'j':       /* jump to linenumber */
436                         if ( lutil_atoi( &jumpline, optarg ) ) {
437                                 usage( tool, progname );
438                         }
439                         break;
440
441                 case 'l':       /* LDIF file */
442                         ldiffile = ch_strdup( optarg );
443                         break;
444
445                 case 'M':
446                         ber_str2bv( optarg, 0, 0, &mech );
447                         break;
448
449                 case 'N':
450                         if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_NORMAL ) {
451                                 usage( tool, progname );
452                         }
453                         dn_mode = SLAP_TOOL_LDAPDN_NORMAL;
454                         break;
455
456                 case 'n':       /* which config file db to index */
457                         if ( lutil_atoi( &dbnum, optarg ) || dbnum < 0 ) {
458                                 usage( tool, progname );
459                         }
460                         break;
461
462                 case 'o':
463                         if ( parse_slapopt( tool, &mode ) ) {
464                                 usage( tool, progname );
465                         }
466                         break;
467
468                 case 'P':
469                         if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_PRETTY ) {
470                                 usage( tool, progname );
471                         }
472                         dn_mode = SLAP_TOOL_LDAPDN_PRETTY;
473                         break;
474
475                 case 'Q':
476                         quiet++;
477                         slap_debug = 0;
478                         break;
479
480                 case 'q':       /* turn on quick */
481                         mode |= SLAP_TOOL_QUICK;
482                         break;
483
484                 case 'R':
485                         realm = optarg;
486                         break;
487
488                 case 'S':
489                         if ( lutil_atou( &csnsid, optarg )
490                                 || csnsid > SLAP_SYNC_SID_MAX )
491                         {
492                                 usage( tool, progname );
493                         }
494                         break;
495
496                 case 's':
497                         switch ( tool ) {
498                         case SLAPADD:
499                         case SLAPMODIFY:
500                                 /* no schema check */
501                                 mode |= SLAP_TOOL_NO_SCHEMA_CHECK;
502                                 break;
503
504                         case SLAPCAT:
505                         case SLAPSCHEMA:
506                                 /* dump subtree */
507                                 subtree = ch_strdup( optarg );
508                                 break;
509                         }
510                         break;
511
512                 case 't':       /* turn on truncate */
513                         truncatemode++;
514                         mode |= SLAP_TRUNCATE_MODE;
515                         break;
516
517                 case 'U':
518                         ber_str2bv( optarg, 0, 0, &authcID );
519                         break;
520
521                 case 'u':       /* dry run */
522                         dryrun++;
523                         break;
524
525                 case 'v':       /* turn on verbose */
526                         verbose++;
527                         break;
528
529                 case 'w':       /* write context csn at the end */
530                         update_ctxcsn++;
531                         break;
532
533                 case 'X':
534                         ber_str2bv( optarg, 0, 0, &authzID );
535                         break;
536
537                 default:
538                         usage( tool, progname );
539                         break;
540                 }
541         }
542
543 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
544         if ( start_syslog ) {
545                 char *logName;
546 #ifdef HAVE_EBCDIC
547                 logName = ch_strdup( progname );
548                 __atoe( logName );
549 #else
550                 logName = (char *)progname;
551 #endif
552
553 #ifdef LOG_LOCAL4
554                 openlog( logName, OPENLOG_OPTIONS, syslogUser );
555 #elif defined LOG_DEBUG
556                 openlog( logName, OPENLOG_OPTIONS );
557 #endif
558 #ifdef HAVE_EBCDIC
559                 free( logName );
560                 logName = NULL;
561 #endif
562         }
563 #endif /* LDAP_DEBUG && LDAP_SYSLOG */
564
565         switch ( tool ) {
566         case SLAPCAT:
567         case SLAPSCHEMA:
568                 writer = 1;
569                 break;
570
571         default:
572                 writer = 0;
573                 break;
574         }
575
576         switch ( tool ) {
577         case SLAPADD:
578         case SLAPCAT:
579         case SLAPMODIFY:
580         case SLAPSCHEMA:
581                 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
582                         usage( tool, progname );
583                 }
584
585                 break;
586
587         case SLAPINDEX:
588                 if ( dbnum >= 0 && base.bv_val != NULL ) {
589                         usage( tool, progname );
590                 }
591
592                 break;
593
594         case SLAPDN:
595                 if ( argc == optind ) {
596                         usage( tool, progname );
597                 }
598                 break;
599
600         case SLAPAUTH:
601                 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
602                         usage( tool, progname );
603                 }
604                 break;
605
606         case SLAPTEST:
607                 if ( argc != optind ) {
608                         usage( tool, progname );
609                 }
610                 break;
611
612         case SLAPACL:
613                 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
614                         usage( tool, progname );
615                 }
616                 if ( BER_BVISNULL( &base ) ) {
617                         usage( tool, progname );
618                 }
619                 ber_dupbv( &baseDN, &base );
620                 break;
621
622         default:
623                 break;
624         }
625
626         if ( ldiffile == NULL ) {
627                 dummy.fp = writer ? stdout : stdin;
628                 ldiffp = &dummy;
629
630         } else if ((ldiffp = ldif_open( ldiffile, writer ? "w" : "r" ))
631                 == NULL )
632         {
633                 perror( ldiffile );
634                 exit( EXIT_FAILURE );
635         }
636
637         /*
638          * initialize stuff and figure out which backend we're dealing with
639          */
640
641         rc = slap_init( mode, progname );
642         if ( rc != 0 ) {
643                 fprintf( stderr, "%s: slap_init failed!\n", progname );
644                 exit( EXIT_FAILURE );
645         }
646
647         rc = read_config( conffile, confdir );
648
649         if ( rc != 0 ) {
650                 fprintf( stderr, "%s: bad configuration %s!\n",
651                         progname, confdir ? "directory" : "file" );
652                 exit( EXIT_FAILURE );
653         }
654
655         if ( debug_unknowns ) {
656                 rc = parse_debug_unknowns( debug_unknowns, &slap_debug );
657                 ldap_charray_free( debug_unknowns );
658                 debug_unknowns = NULL;
659                 if ( rc )
660                         exit( EXIT_FAILURE );
661         }
662
663 #if defined(LDAP_SYSLOG) && defined(LDAP_DEBUG)
664         if ( syslog_unknowns ) {
665                 rc = parse_debug_unknowns( syslog_unknowns, &ldap_syslog );
666                 ldap_charray_free( syslog_unknowns );
667                 syslog_unknowns = NULL;
668                 if ( rc )
669                         exit( EXIT_FAILURE );
670         }
671 #endif
672
673         at_oc_cache = 1;
674
675         switch ( tool ) {
676         case SLAPADD:
677         case SLAPCAT:
678         case SLAPINDEX:
679         case SLAPMODIFY:
680         case SLAPSCHEMA:
681                 if ( !nbackends ) {
682                         fprintf( stderr, "No databases found "
683                                         "in config file\n" );
684                         exit( EXIT_FAILURE );
685                 }
686                 break;
687
688         default:
689                 break;
690         }
691
692         if ( use_glue ) {
693                 rc = glue_sub_attach( 0 );
694
695                 if ( rc != 0 ) {
696                         fprintf( stderr,
697                                 "%s: subordinate configuration error\n", progname );
698                         exit( EXIT_FAILURE );
699                 }
700         }
701
702         rc = slap_schema_check();
703
704         if ( rc != 0 ) {
705                 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
706                 exit( EXIT_FAILURE );
707         }
708
709         switch ( tool ) {
710         case SLAPTEST:
711                 if ( dbnum >= 0 )
712                         goto get_db;
713                 /* FALLTHRU */
714         case SLAPDN:
715         case SLAPAUTH:
716                 be = NULL;
717                 goto startup;
718
719         default:
720                 break;
721         }
722
723         if( filterstr ) {
724                 filter = str2filter( filterstr );
725
726                 if( filter == NULL ) {
727                         fprintf( stderr, "Invalid filter '%s'\n", filterstr );
728                         exit( EXIT_FAILURE );
729                 }
730
731                 ch_free( filterstr );
732                 filterstr = NULL;
733         }
734
735         if( subtree ) {
736                 struct berval val;
737                 ber_str2bv( subtree, 0, 0, &val );
738                 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
739                 if( rc != LDAP_SUCCESS ) {
740                         fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
741                         exit( EXIT_FAILURE );
742                 }
743
744                 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
745                         base = val;
746                 } else {
747                         free( subtree );
748                         subtree = NULL;
749                 }
750         }
751
752         if( base.bv_val != NULL ) {
753                 struct berval nbase;
754
755                 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
756                 if( rc != LDAP_SUCCESS ) {
757                         fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
758                                 progname, base.bv_val );
759                         exit( EXIT_FAILURE );
760                 }
761
762                 be = select_backend( &nbase, 0 );
763                 ber_memfree( nbase.bv_val );
764                 BER_BVZERO( &nbase );
765
766                 switch ( tool ) {
767                 case SLAPACL:
768                         goto startup;
769
770                 default:
771                         break;
772                 }
773
774                 if( be == NULL ) {
775                         fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
776                                 progname, base.bv_val );
777                         exit( EXIT_FAILURE );
778                 }
779                 /* If the named base is a glue master, operate on the
780                  * entire context
781                  */
782                 if ( SLAP_GLUE_INSTANCE( be ) ) {
783                         nosubordinates = 1;
784                 }
785
786                 ch_free( base.bv_val );
787                 BER_BVZERO( &base );
788
789         } else if ( dbnum == -1 ) {
790                 /* no suffix and no dbnum specified, just default to
791                  * the first available database
792                  */
793                 if ( nbackends <= 0 ) {
794                         fprintf( stderr, "No available databases\n" );
795                         exit( EXIT_FAILURE );
796                 }
797                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
798                         dbnum++;
799
800                         /* db #0 is cn=config, don't select it as a default */
801                         if ( dbnum < 1 ) continue;
802                 
803                         if ( SLAP_MONITOR(be))
804                                 continue;
805
806                 /* If just doing the first by default and it is a
807                  * glue subordinate, find the master.
808                  */
809                         if ( SLAP_GLUE_SUBORDINATE(be) ) {
810                                 nosubordinates = 1;
811                                 continue;
812                         }
813                         break;
814                 }
815
816                 if ( !be ) {
817                         fprintf( stderr, "Available database(s) "
818                                         "do not allow %s\n", progname );
819                         exit( EXIT_FAILURE );
820                 }
821                 
822                 if ( nosubordinates == 0 && dbnum > 1 ) {
823                         Debug( LDAP_DEBUG_ANY,
824                                 "The first database does not allow %s;"
825                                 " using the first available one (%d)\n",
826                                 progname, dbnum, 0 );
827                 }
828
829         } else if ( dbnum >= nbackends ) {
830                 fprintf( stderr,
831                         "Database number selected via -n is out of range\n"
832                         "Must be in the range 0 to %d"
833                         " (the number of configured databases)\n",
834                         nbackends - 1 );
835                 exit( EXIT_FAILURE );
836
837         } else {
838 get_db:
839                 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
840                         if ( dbnum == 0 ) break;
841                         dbnum--;
842                 }
843         }
844
845         if ( scope != LDAP_SCOPE_DEFAULT && BER_BVISNULL( &sub_ndn ) ) {
846                 if ( be && be->be_nsuffix ) {
847                         ber_dupbv( &sub_ndn, be->be_nsuffix );
848
849                 } else {
850                         fprintf( stderr,
851                                 "<scope> needs a DN or a valid database\n" );
852                         exit( EXIT_FAILURE );
853                 }
854         }
855
856 startup:;
857         if ( be ) {
858                 BackendDB *bdtmp;
859
860                 dbnum = 0;
861                 LDAP_STAILQ_FOREACH( bdtmp, &backendDB, be_next ) {
862                         if ( bdtmp == be ) break;
863                         dbnum++;
864                 }
865         }
866
867 #ifdef CSRIMALLOC
868         mal_leaktrace(1);
869 #endif
870
871         if ( conffile != NULL ) {
872                 ch_free( conffile );
873                 conffile = NULL;
874         }
875
876         if ( confdir != NULL ) {
877                 ch_free( confdir );
878                 confdir = NULL;
879         }
880
881         if ( ldiffile != NULL ) {
882                 ch_free( ldiffile );
883                 ldiffile = NULL;
884         }
885
886         /* slapdn doesn't specify a backend to startup */
887         if ( !dryrun && tool != SLAPDN ) {
888                 need_shutdown = 1;
889
890                 if ( slap_startup( be ) ) {
891                         switch ( tool ) {
892                         case SLAPTEST:
893                                 fprintf( stderr, "slap_startup failed "
894                                                 "(test would succeed using "
895                                                 "the -u switch)\n" );
896                                 break;
897
898                         default:
899                                 fprintf( stderr, "slap_startup failed\n" );
900                                 break;
901                         }
902
903                         exit( EXIT_FAILURE );
904                 }
905         }
906 }
907
908 int slap_tool_destroy( void )
909 {
910         int rc = 0;
911         if ( !dryrun ) {
912                 if ( need_shutdown ) {
913                         if ( slap_shutdown( be ))
914                                 rc = EXIT_FAILURE;
915                 }
916                 if ( slap_destroy())
917                         rc = EXIT_FAILURE;
918         }
919 #ifdef SLAPD_MODULES
920         if ( slapMode == SLAP_SERVER_MODE ) {
921         /* always false. just pulls in necessary symbol references. */
922                 lutil_uuidstr(NULL, 0);
923         }
924         module_kill();
925 #endif
926         schema_destroy();
927 #ifdef HAVE_TLS
928         ldap_pvt_tls_destroy();
929 #endif
930         config_destroy();
931
932 #ifdef CSRIMALLOC
933         mal_dumpleaktrace( leakfile );
934 #endif
935
936         if ( !BER_BVISNULL( &authcDN ) ) {
937                 ch_free( authcDN.bv_val );
938                 BER_BVZERO( &authcDN );
939         }
940
941         if ( ldiffp && ldiffp != &dummy ) {
942                 ldif_close( ldiffp );
943         }
944         return rc;
945 }
946
947