2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2013 The OpenLDAP Foundation.
5 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
6 * Portions Copyright 2003 IBM Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Kurt Zeilenga for inclusion
19 * in OpenLDAP Software. Additional signficant contributors include
27 #include <ac/stdlib.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
32 #include "slapcommon.h"
35 static volatile sig_atomic_t gotsig;
38 slapcat_sig( int sig )
44 slapcat( int argc, char **argv )
47 int rc = EXIT_SUCCESS;
49 const char *progname = "slapcat";
53 slap_tool_init( progname, SLAPCAT, argc, argv );
55 requestBSF = ( sub_ndn.bv_len || filter );
58 (void) SIGNAL( SIGPIPE, slapcat_sig );
61 (void) SIGNAL( SIGHUP, slapcat_sig );
63 (void) SIGNAL( SIGINT, slapcat_sig );
64 (void) SIGNAL( SIGTERM, slapcat_sig );
66 if( !be->be_entry_open ||
67 !be->be_entry_close ||
68 !( be->be_entry_first_x || be->be_entry_first ) ||
72 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
77 if( be->be_entry_open( be, 0 ) != 0 ) {
78 fprintf( stderr, "%s: could not open database.\n",
84 if ( !requestBSF && be->be_entry_first ) {
85 id = be->be_entry_first( be );
88 if ( be->be_entry_first_x ) {
89 id = be->be_entry_first_x( be,
90 sub_ndn.bv_len ? &sub_ndn : NULL, scope, filter );
93 assert( be->be_entry_first != NULL );
95 id = be->be_entry_first( be );
99 for ( ; id != NOID; id = be->be_entry_next( be ) )
108 e = be->be_entry_get( be, id );
110 printf("# no data for entry id=%08lx\n\n", (long) id );
112 if ( continuemode == 0 ) {
115 } else if ( continuemode == 1 ) {
119 /* this is a last resort: linearly scan all ids
120 * trying to recover as much as possible (ITS#6482) */
121 while ( ++id != NOID ) {
122 e = be->be_entry_get( be, id );
123 if ( e != NULL ) break;
124 printf("# no data for entry id=%08lx\n\n", (long) id );
127 if ( e == NULL ) break;
131 if ( sub_ndn.bv_len && !dnIsSuffixScope( &e->e_nname, &sub_ndn, scope ) )
133 be_entry_release_r( &op, e );
138 if ( filter != NULL ) {
139 int rc = test_filter( NULL, e, filter );
140 if ( rc != LDAP_COMPARE_TRUE ) {
141 be_entry_release_r( &op, e );
148 printf( "# id=%08lx\n", (long) id );
151 data = entry2str_wrap( e, &len, ldif_wrap );
152 be_entry_release_r( &op, e );
154 if ( data == NULL ) {
155 printf("# bad data for entry id=%08lx\n\n", (long) id );
157 if( continuemode ) continue;
161 if ( fputs( data, ldiffp->fp ) == EOF ||
162 fputs( "\n", ldiffp->fp ) == EOF ) {
163 fprintf(stderr, "%s: error writing output.\n",
170 be->be_entry_close( be );
172 if ( slap_tool_destroy())