Back to index

lightning-sunbird  0.9+nobinonly
getfilt.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 /*
00039  * Search for a user-provided string, using a filter configuration file.
00040  * The filter config file controls which type of search is done, based
00041  * on what the user types.
00042  */
00043 
00044 #include "examples.h"
00045 
00046 int
00047 main( int argc, char **argv )
00048 {
00049     LDAP             *ld;
00050     LDAPMessage             *result, *e;
00051     BerElement              *ber;
00052     char             *a, *dn;
00053     char             **vals;
00054     int                     i;
00055     LDAPFiltDesc     *ldfp;
00056     LDAPFiltInfo     *ldfi;
00057     char             buf[ 80 ];
00058     int                     found;
00059 
00060     /* Open the filter config file */
00061     if (( ldfp = ldap_init_getfilter( MY_FILTERFILE )) == NULL ) {
00062        fprintf( stderr, "Can't open filter file \"%s\"\n", MY_FILTERFILE );
00063        return( 1 );
00064     }
00065     /* Read a string to search for */
00066     printf( "Enter a string to search for: " );
00067     gets( buf );
00068     if ( strlen( buf ) == 0 ) {
00069        fprintf( stderr, "usage: %s search-string\n", argv[ 0 ]);
00070        return( 1 );
00071     }
00072     /* get a handle to an LDAP connection */
00073     printf( "Initializing LDAP library... " ); fflush( stdout );
00074     if ( (ld = ldap_init( MY_HOST, MY_PORT )) == NULL ) {
00075        perror( "ldap_init" );
00076        return( 1 );
00077     }
00078     printf( "done.\n" ); fflush( stdout );
00079     /* authenticate to the directory as nobody */
00080     printf( "Binding to the directory... " ); fflush( stdout );
00081     if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
00082        ldap_perror( ld, "ldap_simple_bind_s" );
00083        return( 1 );
00084     }
00085     printf( "done.\n" ); fflush( stdout );
00086     /* search for what the user typed */
00087     found = 0;
00088     for ( ldfi = ldap_getfirstfilter( ldfp, MY_FILTERTAG, buf );
00089            ldfi != NULL; ldfi = ldap_getnextfilter( ldfp )) {
00090        printf( "Filter: \"%s\"... ", ldfi->lfi_filter );
00091        fflush( stdout );
00092        if ( ldap_search_s( ld, MY_SEARCHBASE, ldfi->lfi_scope,
00093               ldfi->lfi_filter, NULL, 0, &result ) != LDAP_SUCCESS ) {
00094            ldap_perror( ld, "ldap_search_s" );
00095            return( 1 );
00096        } else {
00097            found = ldap_count_entries( ld, result );
00098            printf( "done.  Entries found: %d\n", found ); fflush( stdout );
00099            if ( found > 0 ) {
00100               /* Matching entries found - break */
00101               break;
00102            } else {
00103               ldap_msgfree( result );
00104            }
00105        }
00106     }
00107     if ( found == 0  ) {
00108        printf( "No matching entries found\n" );
00109     } else {
00110        printf( "Found %d %s match%s for \"%s\"\n\n", found,
00111               ldfi->lfi_desc, found == 1 ? "" : "es", buf );
00112        /* for each entry print out name + all attrs and values */
00113        for ( e = ldap_first_entry( ld, result ); e != NULL;
00114               e = ldap_next_entry( ld, e ) ) {
00115            if ( (dn = ldap_get_dn( ld, e )) != NULL ) {
00116               printf( "dn: %s\n", dn );
00117               ldap_memfree( dn );
00118            }
00119            for ( a = ldap_first_attribute( ld, e, &ber );
00120                   a != NULL; a = ldap_next_attribute( ld, e, ber ) ) {
00121               if (( vals = ldap_get_values( ld, e, a )) != NULL ) {
00122                   for ( i = 0; vals[i] != NULL; i++ ) {
00123                      printf( "%s: %s\n", a, vals[i] );
00124                   }
00125                   ldap_value_free( vals );
00126               }
00127               ldap_memfree( a );
00128            }
00129            if ( ber != NULL ) {
00130               ber_free( ber, 0 );
00131            }
00132            printf( "\n" );
00133        }
00134        ldap_msgfree( result );
00135     }
00136     ldap_getfilter_free( ldfp );
00137     ldap_unbind( ld );
00138     return( 0 );
00139 }