]> git.sur5r.net Git - openldap/blob - servers/slapd/slapschema.c
Cleanup
[openldap] / servers / slapd / slapschema.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2009 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 Pierangelo Masarati for inclusion
19  * in OpenLDAP Software.  Code portions borrowed from slapcat.c;
20  * contributors are Kurt Zeilenga and 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 slapschema( int argc, char **argv )
45 {
46         ID id;
47         int rc = EXIT_SUCCESS;
48         const char *progname = "slapschema";
49         Connection conn = { 0 };
50         OperationBuffer opbuf;
51         Operation *op = NULL;
52         void *thrctx;
53
54         slap_tool_init( progname, SLAPCAT, argc, argv );
55
56 #ifdef SIGPIPE
57         (void) SIGNAL( SIGPIPE, slapcat_sig );
58 #endif
59 #ifdef SIGHUP
60         (void) SIGNAL( SIGHUP, slapcat_sig );
61 #endif
62         (void) SIGNAL( SIGINT, slapcat_sig );
63         (void) SIGNAL( SIGTERM, slapcat_sig );
64
65         if( !be->be_entry_open ||
66                 !be->be_entry_close ||
67                 !be->be_entry_first ||
68                 !be->be_entry_next ||
69                 !be->be_entry_get )
70         {
71                 fprintf( stderr, "%s: database doesn't support necessary operations.\n",
72                         progname );
73                 exit( EXIT_FAILURE );
74         }
75
76         if( be->be_entry_open( be, 0 ) != 0 ) {
77                 fprintf( stderr, "%s: could not open database.\n",
78                         progname );
79                 exit( EXIT_FAILURE );
80         }
81
82         thrctx = ldap_pvt_thread_pool_context();
83         connection_fake_init( &conn, &opbuf, thrctx );
84         op = &opbuf.ob_op;
85         op->o_tmpmemctx = NULL;
86         op->o_bd = be;
87
88         for ( id = be->be_entry_first( be );
89                 id != NOID;
90                 id = be->be_entry_next( be ) )
91         {
92                 Entry* e;
93                 char textbuf[SLAP_TEXT_BUFLEN];
94                 size_t textlen = sizeof(textbuf);
95                 const char *text = NULL;
96
97                 if ( gotsig )
98                         break;
99
100                 e = be->be_entry_get( be, id );
101                 if ( e == NULL ) {
102                         printf("# no data for entry id=%08lx\n\n", (long) id );
103                         rc = EXIT_FAILURE;
104                         if( continuemode ) continue;
105                         break;
106                 }
107
108                 if( sub_ndn.bv_len && !dnIsSuffix( &e->e_nname, &sub_ndn ) ) {
109                         be_entry_release_r( op, e );
110                         continue;
111                 }
112
113                 if( filter != NULL ) {
114                         int rc = test_filter( NULL, e, filter );
115                         if( rc != LDAP_COMPARE_TRUE ) {
116                                 be_entry_release_r( op, e );
117                                 continue;
118                         }
119                 }
120
121                 if( verbose ) {
122                         printf( "# id=%08lx\n", (long) id );
123                 }
124
125                 rc = entry_schema_check( op, e, NULL, 0, 0, NULL,
126                         &text, textbuf, textlen );
127                 if ( rc != LDAP_SUCCESS ) {
128                         fprintf( ldiffp->fp, "# (%d) %s%s%s\n",
129                                 rc, ldap_err2string( rc ),
130                                 text ? ": " : "",
131                                 text ? text : "" );
132                         fprintf( ldiffp->fp, "dn: %s\n\n", e->e_name.bv_val );
133                 }
134
135                 be_entry_release_r( op, e );
136         }
137
138         be->be_entry_close( be );
139
140         if ( slap_tool_destroy() )
141                 rc = EXIT_FAILURE;
142
143         return rc;
144 }