1 /* slapcommon.c - common routine for the slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2004 The OpenLDAP Foundation.
6 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
7 * Portions Copyright 2003 IBM Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
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>.
19 * This work was initially developed by Kurt Zeilenga for inclusion
20 * in OpenLDAP Software. Additional signficant contributors include
22 * Hallvard B. Furuseth
31 #include <ac/stdlib.h>
33 #include <ac/string.h>
34 #include <ac/socket.h>
35 #include <ac/unistd.h>
37 #include "slapcommon.h"
40 tool_vars tool_globals;
43 static char *leakfilename;
44 static FILE *leakfile;
48 usage( int tool, const char *progname )
52 "usage: %s [-v] [-c] [-d debuglevel] [-f configfile]\n",
57 options = "\t[-n databasenumber | -b suffix]\n"
58 "\t[-l ldiffile] [-u] [-p [-w] | -r [-i syncreplidlist] [-w]]\n";
62 options = "\t[-n databasenumber | -b suffix]"
63 " [-l ldiffile] [-a filter] [-m] [-k]\n";
67 options = "\tDN [...]\n";
71 options = "\t[-n databasenumber | -b suffix]\n";
75 options = "\t[-U authcID] [-X authzID] ID [...]\n";
79 options = "\t[-U authcID | -D authcDN]"
80 " -b DN [attr[/access][:value]] [...]\n";
84 if ( options != NULL ) {
85 fputs( options, stderr );
92 * slap_tool_init - initialize slap utility, handle program options.
96 * argc, argv command line arguments
101 const char* progname,
103 int argc, char **argv )
106 char *conffile = SLAPD_DEFAULT_CONFIGFILE;
107 struct berval base = BER_BVNULL;
108 char *filterstr = NULL;
109 char *subtree = NULL;
110 char *ldiffile = NULL;
112 int mode = SLAP_TOOL_MODE;
113 int truncatemode = 0;
116 leakfilename = malloc( strlen( progname ) + STRLEOF( ".leak" ) - 1 );
117 sprintf( leakfilename, "%s.leak", progname );
118 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
121 free( leakfilename );
126 options = "b:cd:f:i:l:n:prtuvWw";
130 options = "a:b:cd:f:kl:mn:s:v";
131 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
137 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
141 options = "d:f:U:vX:";
142 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
146 options = "b:cd:f:n:v";
147 mode |= SLAP_TOOL_READMAIN;
151 options = "b:D:d:f:U:v";
152 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
156 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
157 exit( EXIT_FAILURE );
161 while ( (i = getopt( argc, argv, options )) != EOF ) {
164 filterstr = strdup( optarg );
168 ber_str2bv( optarg, 0, 1, &base );
171 case 'c': /* enable continue mode */
175 case 'd': /* turn on debugging */
176 ldap_debug += atoi( optarg );
180 ber_str2bv( optarg, 0, 1, &authcDN );
183 case 'f': /* specify a conf file */
184 conffile = strdup( optarg );
187 case 'i': /* specify syncrepl id list */
188 replica_id_string = strdup( optarg );
189 if ( !isdigit( (unsigned char) *replica_id_string )) {
190 usage( tool, progname );
191 exit( EXIT_FAILURE );
193 slap_str2clist( &replica_id_strlist, replica_id_string, "," );
194 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) ;
195 replica_id_list = ch_calloc( i + 1, sizeof( int ) );
196 for ( i = 0; replica_id_strlist && replica_id_strlist[i]; i++ ) {
197 replica_id_list[i] = atoi( replica_id_strlist[i] );
198 if ( replica_id_list[i] >= 1000 ) {
200 "%s: syncrepl id %d is out of range [0..999]\n",
201 progname, replica_id_list[i] );
202 exit( EXIT_FAILURE );
205 replica_id_list[i] = -1;
208 case 'k': /* Retrieve sync cookie entry */
209 retrieve_synccookie = 1;
212 case 'l': /* LDIF file */
213 ldiffile = strdup( optarg );
216 case 'm': /* Retrieve ldapsync entry */
220 case 'n': /* which config file db to index */
221 dbnum = atoi( optarg ) - 1;
224 case 'p': /* replica promotion */
225 replica_promotion = 1;
228 case 'r': /* replica demotion */
229 replica_demotion = 1;
232 case 's': /* dump subtree */
233 subtree = strdup( optarg );
236 case 't': /* turn on truncate */
238 mode |= SLAP_TRUNCATE_MODE;
242 ber_str2bv( optarg, 0, 0, &authcID );
245 case 'u': /* dry run */
249 case 'v': /* turn on verbose */
253 case 'W': /* write context csn on every entry add */
254 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
255 /* FIXME : update_ctxcsn = SLAP_TOOL_CTXCSN_ENTRY; */
258 case 'w': /* write context csn on at the end */
259 update_ctxcsn = SLAP_TOOL_CTXCSN_BATCH;
263 ber_str2bv( optarg, 0, 0, &authzID );
267 usage( tool, progname );
276 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
277 usage( tool, progname );
280 if ( replica_promotion && replica_demotion ) {
281 usage( tool, progname );
283 } else if ( !replica_promotion && !replica_demotion ) {
284 if ( update_ctxcsn != SLAP_TOOL_CTXCSN_KEEP ) {
285 usage( tool, progname );
291 if ( argc == optind ) {
292 usage( tool, progname );
297 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
298 usage( tool, progname );
303 if ( argc != optind ) {
304 usage( tool, progname );
309 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
310 usage( tool, progname );
312 if ( BER_BVISNULL( &base ) ) {
313 usage( tool, progname );
315 ber_dupbv( &baseDN, &base );
322 if ( ldiffile == NULL ) {
323 ldiffp = tool == SLAPCAT ? stdout : stdin;
325 } else if ((ldiffp = fopen( ldiffile, tool == SLAPCAT ? "w" : "r" ))
329 exit( EXIT_FAILURE );
333 * initialize stuff and figure out which backend we're dealing with
337 if ( module_init() != 0 ) {
338 fprintf( stderr, "%s: module_init failed!\n", progname );
339 exit( EXIT_FAILURE );
343 rc = slap_init( mode, progname );
346 fprintf( stderr, "%s: slap_init failed!\n", progname );
347 exit( EXIT_FAILURE );
350 rc = slap_schema_init();
353 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
354 exit( EXIT_FAILURE );
357 if ( frontend_init() ) {
358 fprintf( stderr, "%s: frontend_init failed!\n", progname );
359 exit( EXIT_FAILURE );
362 if ( overlay_init() ) {
363 fprintf( stderr, "%s: overlay_init failed!\n", progname );
364 exit( EXIT_FAILURE );
367 rc = read_config( conffile, 0 );
370 fprintf( stderr, "%s: bad configuration file!\n", progname );
371 exit( EXIT_FAILURE );
381 fprintf( stderr, "No databases found "
382 "in config file\n" );
383 exit( EXIT_FAILURE );
391 rc = glue_sub_init();
394 fprintf( stderr, "Subordinate configuration error\n" );
395 exit( EXIT_FAILURE );
398 rc = slap_schema_check();
401 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
402 exit( EXIT_FAILURE );
417 filter = str2filter( filterstr );
419 if( filter == NULL ) {
420 fprintf( stderr, "Invalid filter '%s'\n", filterstr );
421 exit( EXIT_FAILURE );
427 ber_str2bv( subtree, 0, 0, &val );
428 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
429 if( rc != LDAP_SUCCESS ) {
430 fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
431 exit( EXIT_FAILURE );
434 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
441 if( base.bv_val != NULL ) {
444 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
445 if( rc != LDAP_SUCCESS ) {
446 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
447 progname, base.bv_val );
448 exit( EXIT_FAILURE );
451 be = select_backend( &nbase, 0, 0 );
452 ber_memfree( nbase.bv_val );
463 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
464 progname, base.bv_val );
465 exit( EXIT_FAILURE );
467 /* If the named base is a glue master, operate on the
470 if (SLAP_GLUE_INSTANCE(be)) {
474 } else if ( dbnum == -1 ) {
475 if ( nbackends <= 0 ) {
476 fprintf( stderr, "No available databases\n" );
477 exit( EXIT_FAILURE );
480 be = &backends[dbnum=0];
481 /* If just doing the first by default and it is a
482 * glue subordinate, find the master.
484 while (SLAP_GLUE_SUBORDINATE(be) || SLAP_MONITOR(be)) {
485 if (SLAP_GLUE_SUBORDINATE(be)) {
493 if ( dbnum >= nbackends ) {
494 fprintf( stderr, "Available database(s) "
495 "do not allow %s\n", progname );
496 exit( EXIT_FAILURE );
499 if ( nosubordinates == 0 && dbnum > 0 ) {
500 Debug( LDAP_DEBUG_ANY,
501 "The first database does not allow %s;"
502 " using the first available one (%d)\n",
503 progname, dbnum + 1, 0 );
506 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
508 "Database number selected via -n is out of range\n"
509 "Must be in the range 1 to %d"
510 " (number of databases in the config file)\n",
512 exit( EXIT_FAILURE );
515 be = &backends[dbnum];
524 if ( !dryrun && slap_startup( be ) ) {
525 fprintf( stderr, "slap_startup failed\n" );
526 exit( EXIT_FAILURE );
530 void slap_tool_destroy( void )
532 if ( !dryrun && be != NULL ) {
537 if ( slapMode == SLAP_SERVER_MODE ) {
538 /* always false. just pulls in necessary symbol references. */
539 lutil_uuidstr(NULL, 0);
545 ldap_pvt_tls_destroy();
550 mal_dumpleaktrace( leakfile );
553 if ( !BER_BVISNULL( &authcDN ) ) {
554 ch_free( authcDN.bv_val );