1 /*	$NetBSD: slapschema.c,v 1.1.1.2 2010/12/12 15:22:48 adam Exp $	*/
2 
3 /* OpenLDAP: pkg/ldap/servers/slapd/slapschema.c,v 1.1.2.5 2010/04/14 22:59:10 quanah Exp */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2010 The OpenLDAP Foundation.
7  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
8  * Portions Copyright 2003 IBM Corporation.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted only as authorized by the OpenLDAP
13  * Public License.
14  *
15  * A copy of this license is available in file LICENSE in the
16  * top-level directory of the distribution or, alternatively, at
17  * <http://www.OpenLDAP.org/license.html>.
18  */
19 /* ACKNOWLEDGEMENTS:
20  * This work was initially developed by Pierangelo Masarati for inclusion
21  * in OpenLDAP Software.  Code portions borrowed from slapcat.c;
22  * contributors are Kurt Zeilenga and Jong Hyuk Choi
23  */
24 
25 #include "portable.h"
26 
27 #include <stdio.h>
28 
29 #include "ac/stdlib.h"
30 #include "ac/ctype.h"
31 #include "ac/socket.h"
32 #include "ac/string.h"
33 
34 #include "slapcommon.h"
35 #include "ldif.h"
36 
37 static volatile sig_atomic_t gotsig;
38 
39 static RETSIGTYPE
40 slapcat_sig( int sig )
41 {
42 	gotsig=1;
43 }
44 
45 int
46 slapschema( int argc, char **argv )
47 {
48 	ID id;
49 	int rc = EXIT_SUCCESS;
50 	const char *progname = "slapschema";
51 	Connection conn = { 0 };
52 	OperationBuffer	opbuf;
53 	Operation *op = NULL;
54 	void *thrctx;
55 	int requestBSF = 0;
56 	int doBSF = 0;
57 
58 	slap_tool_init( progname, SLAPCAT, argc, argv );
59 
60 	requestBSF = ( sub_ndn.bv_len || filter );
61 
62 #ifdef SIGPIPE
63 	(void) SIGNAL( SIGPIPE, slapcat_sig );
64 #endif
65 #ifdef SIGHUP
66 	(void) SIGNAL( SIGHUP, slapcat_sig );
67 #endif
68 	(void) SIGNAL( SIGINT, slapcat_sig );
69 	(void) SIGNAL( SIGTERM, slapcat_sig );
70 
71 	if( !be->be_entry_open ||
72 		!be->be_entry_close ||
73 		!( be->be_entry_first || be->be_entry_first_x ) ||
74 		!be->be_entry_next ||
75 		!be->be_entry_get )
76 	{
77 		fprintf( stderr, "%s: database doesn't support necessary operations.\n",
78 			progname );
79 		exit( EXIT_FAILURE );
80 	}
81 
82 	if( be->be_entry_open( be, 0 ) != 0 ) {
83 		fprintf( stderr, "%s: could not open database.\n",
84 			progname );
85 		exit( EXIT_FAILURE );
86 	}
87 
88 	thrctx = ldap_pvt_thread_pool_context();
89 	connection_fake_init( &conn, &opbuf, thrctx );
90 	op = &opbuf.ob_op;
91 	op->o_tmpmemctx = NULL;
92 	op->o_bd = be;
93 
94 
95 	if ( !requestBSF && be->be_entry_first ) {
96 		id = be->be_entry_first( be );
97 
98 	} else {
99 		if ( be->be_entry_first_x ) {
100 			id = be->be_entry_first_x( be,
101 				sub_ndn.bv_len ? &sub_ndn : NULL, scope, filter );
102 
103 		} else {
104 			assert( be->be_entry_first != NULL );
105 			doBSF = 1;
106 			id = be->be_entry_first( be );
107 		}
108 	}
109 
110 	for ( ; id != NOID; id = be->be_entry_next( be ) ) {
111 		Entry* e;
112 		char textbuf[SLAP_TEXT_BUFLEN];
113 		size_t textlen = sizeof(textbuf);
114 		const char *text = NULL;
115 
116 		if ( gotsig )
117 			break;
118 
119 		e = be->be_entry_get( be, id );
120 		if ( e == NULL ) {
121 			printf("# no data for entry id=%08lx\n\n", (long) id );
122 			rc = EXIT_FAILURE;
123 			if( continuemode ) continue;
124 			break;
125 		}
126 
127 		if ( doBSF ) {
128 			if ( sub_ndn.bv_len && !dnIsSuffixScope( &e->e_nname, &sub_ndn, scope ) )
129 			{
130 				be_entry_release_r( op, e );
131 				continue;
132 			}
133 
134 
135 			if ( filter != NULL ) {
136 				int rc = test_filter( NULL, e, filter );
137 				if ( rc != LDAP_COMPARE_TRUE ) {
138 					be_entry_release_r( op, e );
139 					continue;
140 				}
141 			}
142 		}
143 
144 		if( verbose ) {
145 			printf( "# id=%08lx\n", (long) id );
146 		}
147 
148 		rc = entry_schema_check( op, e, NULL, 0, 0, NULL,
149 			&text, textbuf, textlen );
150 		if ( rc != LDAP_SUCCESS ) {
151 			fprintf( ldiffp->fp, "# (%d) %s%s%s\n",
152 				rc, ldap_err2string( rc ),
153 				text ? ": " : "",
154 				text ? text : "" );
155 			fprintf( ldiffp->fp, "dn: %s\n\n", e->e_name.bv_val );
156 		}
157 
158 		be_entry_release_r( op, e );
159 	}
160 
161 	be->be_entry_close( be );
162 
163 	if ( slap_tool_destroy() )
164 		rc = EXIT_FAILURE;
165 
166 	return rc;
167 }
168