]> git.sur5r.net Git - openldap/blob - servers/slapd/slapcat.c
b8da193d30f96cf205d8ab27a4f7a0d935c51900
[openldap] / servers / slapd / slapcat.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2008 The OpenLDAP Foundation.
5  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
6  * Portions Copyright 2003 IBM Corporation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
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>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Kurt Zeilenga for inclusion
19  * in OpenLDAP Software.  Additional signficant contributors include
20  *    Jong Hyuk Choi
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/stdlib.h>
28 #include <ac/ctype.h>
29 #include <ac/socket.h>
30 #include <ac/string.h>
31
32 #include "slapcommon.h"
33 #include "ldif.h"
34
35 static volatile sig_atomic_t gotsig;
36
37 static RETSIGTYPE
38 slapcat_sig( int sig )
39 {
40         gotsig=1;
41 }
42
43 int
44 slapcat( int argc, char **argv )
45 {
46         ID id;
47         int rc = EXIT_SUCCESS;
48         Operation op = {0};
49         const char *progname = "slapcat";
50
51         slap_tool_init( progname, SLAPCAT, argc, argv );
52
53 #ifdef SIGPIPE
54         (void) SIGNAL( SIGPIPE, slapcat_sig );
55 #endif
56 #ifdef SIGHUP
57         (void) SIGNAL( SIGHUP, slapcat_sig );
58 #endif
59         (void) SIGNAL( SIGINT, slapcat_sig );
60         (void) SIGNAL( SIGTERM, slapcat_sig );
61
62         if( !be->be_entry_open ||
63                 !be->be_entry_close ||
64                 !be->be_entry_first ||
65                 !be->be_entry_next ||
66                 !be->be_entry_get )
67         {
68                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
69                         progname );
70                 exit( EXIT_FAILURE );
71         }
72
73         if( be->be_entry_open( be, 0 ) != 0 ) {
74                 fprintf( stderr, "%s: could not open database.\n",
75                         progname );
76                 exit( EXIT_FAILURE );
77         }
78
79         op.o_bd = be;
80         for ( id = be->be_entry_first( be );
81                 id != NOID;
82                 id = be->be_entry_next( be ) )
83         {
84                 char *data;
85                 int len;
86                 Entry* e;
87                 int writerc;
88
89                 if ( gotsig )
90                         break;
91
92                 e = be->be_entry_get( be, id );
93                 if ( e == NULL ) {
94                         printf("# no data for entry id=%08lx\n\n", (long) id );
95                         rc = EXIT_FAILURE;
96                         if( continuemode ) continue;
97                         break;
98                 }
99
100                 if( sub_ndn.bv_len && !dnIsSuffix( &e->e_nname, &sub_ndn ) ) {
101                         be_entry_release_r( &op, e );
102                         continue;
103                 }
104
105                 if( filter != NULL ) {
106                         int rc = test_filter( NULL, e, filter );
107                         if( rc != LDAP_COMPARE_TRUE ) {
108                                 be_entry_release_r( &op, e );
109                                 continue;
110                         }
111                 }
112
113                 if( verbose ) {
114                         printf( "# id=%08lx\n", (long) id );
115                 }
116
117                 data = entry2str( e, &len );
118                 be_entry_release_r( &op, e );
119
120                 if ( data == NULL ) {
121                         printf("# bad data for entry id=%08lx\n\n", (long) id );
122                         rc = EXIT_FAILURE;
123                         if( continuemode ) continue;
124                         break;
125                 }
126
127                 writerc = fputs( data, ldiffp->fp );
128                 if ( writerc == EOF ) {
129                         fprintf(stderr, "%s: error writing output.\n",
130                                 progname);
131                         rc = EXIT_FAILURE;
132                         break;
133                 }
134
135                 writerc = fputs( "\n", ldiffp->fp );
136                 if ( writerc == EOF ) {
137                         fprintf(stderr, "%s: error writing output.\n",
138                                 progname);
139                         rc = EXIT_FAILURE;
140                         break;
141                 }
142         }
143
144         be->be_entry_close( be );
145
146         slap_tool_destroy();
147         return rc;
148 }