Back to index

lightning-sunbird  0.9+nobinonly
ldapcompare.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /* ldapdelete.c - simple program to delete an entry using LDAP */
00039 
00040 #include "ldaptool.h"
00041 #include "fileurl.h"
00042 
00043 static int    contoper = 0;
00044 static LDAP   *ld;
00045 static int    ldapcompare_quiet = 0;
00046 
00047 static int docompare( LDAP *ld, const char *dn, const char *attrtype,
00048               const struct berval *bvalue, LDAPControl **serverctrls );
00049 static void options_callback( int option, char *optarg );
00050 static int typeval2berval( char *typeval, char **typep, struct berval *bvp );
00051 
00052 
00053 static void
00054 usage( int rc )
00055 {
00056     fprintf( stderr, "usage: %s [options] attributetype:value [dn...]\n",
00057               ldaptool_progname );
00058     fprintf( stderr, "       %s [options] attributetype::base64value [dn...]\n",
00059               ldaptool_progname );
00060     fprintf( stderr, "       %s [options] attributetype:<fileurl [dn...]\n",
00061               ldaptool_progname );
00062     fprintf( stderr, "options:\n" );
00063     ldaptool_common_usage( 0 );
00064     fprintf( stderr, "    -c\t\tcontinuous mode (do not stop on errors)\n" );
00065     fprintf( stderr, "    -f file\tread DNs to compare against from file\n" );
00066     fprintf( stderr, "    -q\t\tbe quiet when comparing entries\n" );
00067     exit( rc );
00068 }
00069 
00070 
00071 int
00072 main( int argc, char **argv )
00073 {
00074     char             buf[ 4096 ], *typeval = NULL, *type = NULL;
00075     struct berval    bv;
00076     int                     rc, optind;
00077     LDAPControl             *ldctrl;
00078 
00079 #ifdef notdef
00080 #ifdef HPUX11
00081 #ifndef __LP64__
00082        _main( argc, argv);
00083 #endif /* __LP64_ */
00084 #endif /* HPUX11 */
00085 #endif
00086 
00087     optind = ldaptool_process_args( argc, argv, "cq", 0, options_callback );
00088 
00089     if ( ldaptool_fp == NULL && optind >= argc ) {
00090        ldaptool_fp = stdin;
00091     }
00092 
00093     ld = ldaptool_ldap_init( 0 );
00094 
00095     ldaptool_bind( ld );
00096 
00097     if (( ldctrl = ldaptool_create_manage_dsait_control()) != NULL ) {
00098        ldaptool_add_control_to_array( ldctrl, ldaptool_request_ctrls);
00099     } 
00100 
00101     if ((ldctrl = ldaptool_create_proxyauth_control(ld)) !=NULL) {
00102        ldaptool_add_control_to_array( ldctrl, ldaptool_request_ctrls);
00103     }
00104 
00105     if ( optind >= argc ) {
00106        usage( LDAP_PARAM_ERROR );
00107     }
00108 
00109     typeval = ldaptool_local2UTF8( argv[optind] );
00110     if (( rc = typeval2berval( typeval, &type, &bv )) != LDAP_SUCCESS ) {
00111        fprintf( stderr, "%s: unable to parse \"%s\"\n",
00112                   ldaptool_progname, argv[optind] );
00113        usage( rc );
00114        free( typeval );
00115     }
00116     ++optind;
00117 
00118     rc = 0;
00119     if ( ldaptool_fp == NULL ) {
00120        for ( ; optind < argc &&
00121               ( contoper || !LDAPTOOL_RESULT_IS_AN_ERROR( rc ) );
00122               ++optind ) {
00123             char *conv;
00124 
00125             conv = ldaptool_local2UTF8( argv[ optind ] );
00126            rc = docompare( ld, conv, type, &bv, ldaptool_request_ctrls );
00127             if ( conv != NULL ) {
00128                 free( conv );
00129            }
00130        }
00131     } else {
00132        while ( contoper || !LDAPTOOL_RESULT_IS_AN_ERROR( rc ) &&
00133               fgets(buf, sizeof(buf), ldaptool_fp) != NULL) {
00134            buf[ strlen( buf ) - 1 ] = '\0';      /* remove trailing newline */
00135            if ( *buf != '\0' ) {
00136               rc = docompare( ld, buf, type, &bv, ldaptool_request_ctrls );
00137            }
00138        }
00139     }
00140 
00141     ldaptool_reset_control_array( ldaptool_request_ctrls );
00142     ldaptool_cleanup( ld );
00143     if ( typeval != NULL ) free( typeval );
00144     if ( bv.bv_val != NULL ) free( bv.bv_val );
00145 
00146     return( rc );
00147 }
00148 
00149 static void
00150 options_callback( int option, char *optarg )
00151 {
00152     switch( option ) {
00153     case 'c': /* continuous operation mode */
00154        ++contoper;
00155        break;
00156     case 'q': /* continuous operation mode */
00157        ++ldapcompare_quiet;
00158        break;
00159     default:
00160        usage( LDAP_PARAM_ERROR );
00161     }
00162 }
00163 
00164 
00165 static int
00166 docompare( LDAP *ld, const char *dn, const char *attrtype,
00167               const struct berval *bvalue, LDAPControl **serverctrls )
00168 {
00169     int              rc;
00170 
00171     if ( !ldapcompare_quiet ) {
00172        char   *valuestr, tmpbuf[256];
00173 
00174        if ( ldaptool_berval_is_ascii( bvalue )) {
00175               valuestr = bvalue->bv_val;
00176        } else {
00177 #ifdef HAVE_SNPRINTF
00178            snprintf( tmpbuf, sizeof(tmpbuf), "NOT ASCII (%ld bytes)",
00179                      bvalue->bv_len );
00180 #else
00181            sprintf( tmpbuf, "NOT ASCII (%ld bytes)",
00182                      bvalue->bv_len );
00183 #endif
00184            valuestr = tmpbuf;
00185        }
00186        printf( "%scomparing type: \"%s\" value: \"%s\" in entry \"%s\"\n",
00187               ldaptool_not ? "!" : "", attrtype, valuestr, dn );
00188     }
00189     if ( ldaptool_not ) {
00190        rc = LDAP_COMPARE_TRUE;
00191     } else {
00192        rc = ldaptool_compare_ext_s( ld, dn, attrtype, bvalue,
00193            serverctrls, NULL, "ldap_compare" );
00194        if ( !ldapcompare_quiet ) {
00195            if ( rc == LDAP_COMPARE_TRUE ) {
00196               puts( "compare TRUE" );
00197            } else if ( rc == LDAP_COMPARE_FALSE ) {
00198               puts( "compare FALSE" );
00199            }
00200        }
00201     }
00202 
00203     return( rc );
00204 }
00205 
00206 
00207 /*
00208  * Parse an ldapcompare type:value or type::value argument.
00209  *
00210  * The *typep is set to point into the typeval string.
00211  * bvp->bv_val is created from malloc'd memory.
00212  *
00213  * This function returns an LDAP error code (LDAP_SUCCESS if all goes well).
00214  */
00215 static int
00216 typeval2berval( char *typeval, char **typep, struct berval *bvp )
00217 {
00218     char      *value;
00219     int              vlen, rc;
00220 
00221     if ( ldif_parse_line( typeval, typep, &value, &vlen ) != 0 ) {
00222        return( LDAP_PARAM_ERROR );
00223     }
00224 
00225     rc = ldaptool_berval_from_ldif_value( value, vlen, bvp,
00226            1 /* recognize file URLs */, 0 /* always try file */,
00227            1 /* report errors */ );
00228 
00229     return( ldaptool_fileurlerr2ldaperr( rc ));
00230 }