1 /* slapcommon.c - common routine for the slap tools */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2005 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"
41 tool_vars tool_globals;
44 static char *leakfilename;
45 static FILE *leakfile;
51 usage( int tool, const char *progname )
55 "usage: %s [-v] [-d debuglevel] [-f configfile] [-F configdir]",
60 options = "\n\t[-U authcID | -D authcDN] [-X authzID | -o authzDN=<DN>]"
61 "\n\t-b DN -o <var>[=<val>] [-u] [attr[/access][:value]] [...]\n";
65 options = " [-c]\n\t[-n databasenumber | -b suffix]\n"
66 "\t[-l ldiffile] [-q] [-u] [-w]\n";
70 options = "\n\t[-U authcID] [-X authzID] [-R realm] [-M mech] ID [...]\n";
74 options = " [-c]\n\t[-n databasenumber | -b suffix]"
75 " [-l ldiffile] [-a filter]\n";
79 options = "\n\t[-N | -P] DN [...]\n";
83 options = " [-c]\n\t[-n databasenumber | -b suffix] [-q]\n";
91 if ( options != NULL ) {
92 fputs( options, stderr );
103 p = strchr( optarg, '=' );
111 if ( strncasecmp( optarg, "sockurl", len ) == 0 ) {
112 if ( !BER_BVISNULL( &listener_url ) ) {
113 ber_memfree( listener_url.bv_val );
115 ber_str2bv( p, 0, 1, &listener_url );
117 } else if ( strncasecmp( optarg, "domain", len ) == 0 ) {
118 if ( !BER_BVISNULL( &peer_domain ) ) {
119 ber_memfree( peer_domain.bv_val );
121 ber_str2bv( p, 0, 1, &peer_domain );
123 } else if ( strncasecmp( optarg, "peername", len ) == 0 ) {
124 if ( !BER_BVISNULL( &peer_name ) ) {
125 ber_memfree( peer_name.bv_val );
127 ber_str2bv( p, 0, 1, &peer_name );
129 } else if ( strncasecmp( optarg, "sockname", len ) == 0 ) {
130 if ( !BER_BVISNULL( &sock_name ) ) {
131 ber_memfree( sock_name.bv_val );
133 ber_str2bv( p, 0, 1, &sock_name );
135 } else if ( strncasecmp( optarg, "ssf", len ) == 0 ) {
138 } else if ( strncasecmp( optarg, "transport_ssf", len ) == 0 ) {
139 transport_ssf = atoi( p );
141 } else if ( strncasecmp( optarg, "tls_ssf", len ) == 0 ) {
144 } else if ( strncasecmp( optarg, "sasl_ssf", len ) == 0 ) {
145 sasl_ssf = atoi( p );
147 } else if ( strncasecmp( optarg, "authzDN", len ) == 0 ) {
148 ber_str2bv( p, 0, 1, &authzDN );
158 * slap_tool_init - initialize slap utility, handle program options.
162 * argc, argv command line arguments
167 const char* progname,
169 int argc, char **argv )
172 char *conffile = NULL;
173 char *confdir = NULL;
174 struct berval base = BER_BVNULL;
175 char *filterstr = NULL;
176 char *subtree = NULL;
177 char *ldiffile = NULL;
179 int mode = SLAP_TOOL_MODE;
180 int truncatemode = 0;
183 leakfilename = malloc( strlen( progname ) + STRLENOF( ".leak" ) + 1 );
184 sprintf( leakfilename, "%s.leak", progname );
185 if( ( leakfile = fopen( leakfilename, "w" )) == NULL ) {
188 free( leakfilename );
193 options = "b:cd:f:F:l:n:qtuvw";
197 options = "a:b:cd:f:F:l:n:s:v";
198 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
202 options = "d:f:F:NPv";
203 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
207 options = "d:f:F:uv";
208 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
212 options = "d:f:F:M:R:U:vX:";
213 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
217 options = "b:cd:f:F:n:qv";
218 mode |= SLAP_TOOL_READMAIN;
222 options = "b:D:d:f:F:o:uU:vX:";
223 mode |= SLAP_TOOL_READMAIN | SLAP_TOOL_READONLY;
227 fprintf( stderr, "%s: unknown tool mode (%d)\n", progname, tool );
228 exit( EXIT_FAILURE );
232 while ( (i = getopt( argc, argv, options )) != EOF ) {
235 filterstr = strdup( optarg );
239 ber_str2bv( optarg, 0, 1, &base );
242 case 'c': /* enable continue mode */
246 case 'd': /* turn on debugging */
247 ldap_debug += atoi( optarg );
251 ber_str2bv( optarg, 0, 1, &authcDN );
254 case 'f': /* specify a conf file */
255 conffile = strdup( optarg );
258 case 'F': /* specify a conf dir */
259 confdir = strdup( optarg );
262 case 'l': /* LDIF file */
263 ldiffile = strdup( optarg );
267 ber_str2bv( optarg, 0, 0, &mech );
271 if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_NORMAL ) {
272 usage( tool, progname );
274 dn_mode = SLAP_TOOL_LDAPDN_NORMAL;
277 case 'n': /* which config file db to index */
278 dbnum = atoi( optarg );
282 if ( parse_slapacl() ) {
283 usage( tool, progname );
288 if ( dn_mode && dn_mode != SLAP_TOOL_LDAPDN_PRETTY ) {
289 usage( tool, progname );
291 dn_mode = SLAP_TOOL_LDAPDN_PRETTY;
294 case 'q': /* turn on quick */
295 mode |= SLAP_TOOL_QUICK;
302 case 's': /* dump subtree */
303 subtree = strdup( optarg );
306 case 't': /* turn on truncate */
308 mode |= SLAP_TRUNCATE_MODE;
312 ber_str2bv( optarg, 0, 0, &authcID );
315 case 'u': /* dry run */
319 case 'v': /* turn on verbose */
323 case 'w': /* write context csn at the end */
328 ber_str2bv( optarg, 0, 0, &authzID );
332 usage( tool, progname );
341 if ( ( argc != optind ) || (dbnum >= 0 && base.bv_val != NULL ) ) {
342 usage( tool, progname );
348 if ( argc == optind ) {
349 usage( tool, progname );
354 if ( argc == optind && BER_BVISNULL( &authcID ) ) {
355 usage( tool, progname );
360 if ( argc != optind ) {
361 usage( tool, progname );
366 if ( !BER_BVISNULL( &authcDN ) && !BER_BVISNULL( &authcID ) ) {
367 usage( tool, progname );
369 if ( BER_BVISNULL( &base ) ) {
370 usage( tool, progname );
372 ber_dupbv( &baseDN, &base );
381 if ( ldiffile == NULL ) {
382 dummy.fp = tool == SLAPCAT ? stdout : stdin;
385 } else if ((ldiffp = ldif_open( ldiffile, tool == SLAPCAT ? "w" : "r" ))
389 exit( EXIT_FAILURE );
393 * initialize stuff and figure out which backend we're dealing with
397 if ( module_init() != 0 ) {
398 fprintf( stderr, "%s: module_init failed!\n", progname );
399 exit( EXIT_FAILURE );
403 rc = slap_schema_init();
406 fprintf( stderr, "%s: slap_schema_init failed!\n", progname );
407 exit( EXIT_FAILURE );
410 rc = slap_init( mode, progname );
413 fprintf( stderr, "%s: slap_init failed!\n", progname );
414 exit( EXIT_FAILURE );
417 if ( frontend_init() ) {
418 fprintf( stderr, "%s: frontend_init failed!\n", progname );
419 exit( EXIT_FAILURE );
422 if ( overlay_init() ) {
423 fprintf( stderr, "%s: overlay_init failed!\n", progname );
424 exit( EXIT_FAILURE );
429 fprintf( stderr, "%s: acl_init failed!\n", progname );
430 exit( EXIT_FAILURE );
432 #endif /* SLAP_DYNACL */
434 rc = read_config( conffile, confdir );
437 fprintf( stderr, "%s: bad configuration %s!\n",
438 progname, confdir ? "directory" : "file" );
439 exit( EXIT_FAILURE );
449 fprintf( stderr, "No databases found "
450 "in config file\n" );
451 exit( EXIT_FAILURE );
459 rc = slap_schema_check();
462 fprintf( stderr, "%s: slap_schema_prep failed!\n", progname );
463 exit( EXIT_FAILURE );
478 filter = str2filter( filterstr );
480 if( filter == NULL ) {
481 fprintf( stderr, "Invalid filter '%s'\n", filterstr );
482 exit( EXIT_FAILURE );
488 ber_str2bv( subtree, 0, 0, &val );
489 rc = dnNormalize( 0, NULL, NULL, &val, &sub_ndn, NULL );
490 if( rc != LDAP_SUCCESS ) {
491 fprintf( stderr, "Invalid subtree DN '%s'\n", subtree );
492 exit( EXIT_FAILURE );
495 if ( BER_BVISNULL( &base ) && dbnum == -1 ) {
502 if( base.bv_val != NULL ) {
505 rc = dnNormalize( 0, NULL, NULL, &base, &nbase, NULL );
506 if( rc != LDAP_SUCCESS ) {
507 fprintf( stderr, "%s: slap_init invalid suffix (\"%s\")\n",
508 progname, base.bv_val );
509 exit( EXIT_FAILURE );
512 be = select_backend( &nbase, 0, 0 );
513 ber_memfree( nbase.bv_val );
524 fprintf( stderr, "%s: slap_init no backend for \"%s\"\n",
525 progname, base.bv_val );
526 exit( EXIT_FAILURE );
528 /* If the named base is a glue master, operate on the
531 if ( SLAP_GLUE_INSTANCE( be ) ) {
535 } else if ( dbnum == -1 ) {
536 /* no suffix and no dbnum specified, just default to
537 * the first available database
539 if ( nbackends <= 0 ) {
540 fprintf( stderr, "No available databases\n" );
541 exit( EXIT_FAILURE );
543 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
546 /* db #0 is cn=config, don't select it as a default */
547 if ( dbnum < 1 ) continue;
549 if ( SLAP_MONITOR(be))
552 /* If just doing the first by default and it is a
553 * glue subordinate, find the master.
555 if ( SLAP_GLUE_SUBORDINATE(be) ) {
563 fprintf( stderr, "Available database(s) "
564 "do not allow %s\n", progname );
565 exit( EXIT_FAILURE );
568 if ( nosubordinates == 0 && dbnum > 1 ) {
569 Debug( LDAP_DEBUG_ANY,
570 "The first database does not allow %s;"
571 " using the first available one (%d)\n",
572 progname, dbnum, 0 );
575 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
577 "Database number selected via -n is out of range\n"
578 "Must be in the range 0 to %d"
579 " (number of configured databases)\n",
581 exit( EXIT_FAILURE );
584 LDAP_STAILQ_FOREACH( be, &backendDB, be_next ) {
585 if ( dbnum == 0 ) break;
596 if ( conffile != NULL ) {
600 if ( ldiffile != NULL ) {
604 /* slapdn doesn't specify a backend to startup */
605 if ( !dryrun && tool != SLAPDN && slap_startup( be ) ) {
608 fprintf( stderr, "slap_startup failed "
609 "(test would succeed using "
610 "the -u switch)\n" );
614 fprintf( stderr, "slap_startup failed\n" );
618 exit( EXIT_FAILURE );
622 void slap_tool_destroy( void )
629 if ( slapMode == SLAP_SERVER_MODE ) {
630 /* always false. just pulls in necessary symbol references. */
631 lutil_uuidstr(NULL, 0);
637 ldap_pvt_tls_destroy();
642 mal_dumpleaktrace( leakfile );
645 if ( !BER_BVISNULL( &authcDN ) ) {
646 ch_free( authcDN.bv_val );