Back to index

openldap  2.4.31
slapschema.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1998-2012 The OpenLDAP Foundation.
00005  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
00006  * Portions Copyright 2003 IBM Corporation.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 /* ACKNOWLEDGEMENTS:
00018  * This work was initially developed by Pierangelo Masarati for inclusion
00019  * in OpenLDAP Software.  Code portions borrowed from slapcat.c;
00020  * contributors are Kurt Zeilenga and Jong Hyuk Choi
00021  */
00022 
00023 #include "portable.h"
00024 
00025 #include <stdio.h>
00026 
00027 #include "ac/stdlib.h"
00028 #include "ac/ctype.h"
00029 #include "ac/socket.h"
00030 #include "ac/string.h"
00031 
00032 #include "slapcommon.h"
00033 #include "ldif.h"
00034 
00035 static volatile sig_atomic_t gotsig;
00036 
00037 static RETSIGTYPE
00038 slapcat_sig( int sig )
00039 {
00040        gotsig=1;
00041 }
00042 
00043 int
00044 slapschema( int argc, char **argv )
00045 {
00046        ID id;
00047        int rc = EXIT_SUCCESS;
00048        const char *progname = "slapschema";
00049        Connection conn = { 0 };
00050        OperationBuffer      opbuf;
00051        Operation *op = NULL;
00052        void *thrctx;
00053        int requestBSF = 0;
00054        int doBSF = 0;
00055 
00056        slap_tool_init( progname, SLAPCAT, argc, argv );
00057 
00058        requestBSF = ( sub_ndn.bv_len || filter );
00059 
00060 #ifdef SIGPIPE
00061        (void) SIGNAL( SIGPIPE, slapcat_sig );
00062 #endif
00063 #ifdef SIGHUP
00064        (void) SIGNAL( SIGHUP, slapcat_sig );
00065 #endif
00066        (void) SIGNAL( SIGINT, slapcat_sig );
00067        (void) SIGNAL( SIGTERM, slapcat_sig );
00068 
00069        if( !be->be_entry_open ||
00070               !be->be_entry_close ||
00071               !( be->be_entry_first || be->be_entry_first_x ) ||
00072               !be->be_entry_next ||
00073               !be->be_entry_get )
00074        {
00075               fprintf( stderr, "%s: database doesn't support necessary operations.\n",
00076                      progname );
00077               exit( EXIT_FAILURE );
00078        }
00079 
00080        if( be->be_entry_open( be, 0 ) != 0 ) {
00081               fprintf( stderr, "%s: could not open database.\n",
00082                      progname );
00083               exit( EXIT_FAILURE );
00084        }
00085 
00086        thrctx = ldap_pvt_thread_pool_context();
00087        connection_fake_init( &conn, &opbuf, thrctx );
00088        op = &opbuf.ob_op;
00089        op->o_tmpmemctx = NULL;
00090        op->o_bd = be;
00091 
00092 
00093        if ( !requestBSF && be->be_entry_first ) {
00094               id = be->be_entry_first( be );
00095 
00096        } else {
00097               if ( be->be_entry_first_x ) {
00098                      id = be->be_entry_first_x( be,
00099                             sub_ndn.bv_len ? &sub_ndn : NULL, scope, filter );
00100 
00101               } else {
00102                      assert( be->be_entry_first != NULL );
00103                      doBSF = 1;
00104                      id = be->be_entry_first( be );
00105               }
00106        }
00107 
00108        for ( ; id != NOID; id = be->be_entry_next( be ) ) {
00109               Entry* e;
00110               char textbuf[SLAP_TEXT_BUFLEN];
00111               size_t textlen = sizeof(textbuf);
00112               const char *text = NULL;
00113 
00114               if ( gotsig )
00115                      break;
00116 
00117               e = be->be_entry_get( be, id );
00118               if ( e == NULL ) {
00119                      printf("# no data for entry id=%08lx\n\n", (long) id );
00120                      rc = EXIT_FAILURE;
00121                      if( continuemode ) continue;
00122                      break;
00123               }
00124 
00125               if ( doBSF ) {
00126                      if ( sub_ndn.bv_len && !dnIsSuffixScope( &e->e_nname, &sub_ndn, scope ) )
00127                      {
00128                             be_entry_release_r( op, e );
00129                             continue;
00130                      }
00131 
00132 
00133                      if ( filter != NULL ) {
00134                             int rc = test_filter( NULL, e, filter );
00135                             if ( rc != LDAP_COMPARE_TRUE ) {
00136                                    be_entry_release_r( op, e );
00137                                    continue;
00138                             }
00139                      }
00140               }
00141 
00142               if( verbose ) {
00143                      printf( "# id=%08lx\n", (long) id );
00144               }
00145 
00146               rc = entry_schema_check( op, e, NULL, 0, 0, NULL,
00147                      &text, textbuf, textlen );
00148               if ( rc != LDAP_SUCCESS ) {
00149                      fprintf( ldiffp->fp, "# (%d) %s%s%s\n",
00150                             rc, ldap_err2string( rc ),
00151                             text ? ": " : "",
00152                             text ? text : "" );
00153                      fprintf( ldiffp->fp, "dn: %s\n\n", e->e_name.bv_val );
00154               }
00155 
00156               be_entry_release_r( op, e );
00157        }
00158 
00159        be->be_entry_close( be );
00160 
00161        if ( slap_tool_destroy() )
00162               rc = EXIT_FAILURE;
00163 
00164        return rc;
00165 }