Back to index

lightning-sunbird  0.9+nobinonly
ssnoauth.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  * Use an SSL connection to search the directory for all people whose
00040  * surname (last name) is "Jensen".  Since the "sn" attribute is a
00041  * caseignorestring (cis), case is not significant when searching.
00042  *
00043  */
00044 
00045 #include "examples.h"
00046 #include "ldap_ssl.h"
00047 #include "ldappr.h"
00048 
00049 #define MY_CERTDB_PATH             "/u/mhein/.netscape/"
00050 
00051 int
00052 main( int argc, char **argv )
00053 {
00054        LDAP          *ld;
00055        LDAPMessage   *result, *e;
00056        BerElement    *ber;
00057        char          *a, *dn;
00058        char          **vals;
00059        int           i;
00060 
00061        /* Initialize the client */
00062        if ( ldapssl_client_init( MY_CERTDB_PATH, NULL ) < 0 ) {
00063               perror( "ldapssl_client_init" );
00064               return( 1 );
00065        }
00066 
00067        /* set the max I/O timeout option to 10 seconds */
00068        if ( prldap_set_session_option( NULL, NULL, PRLDAP_OPT_IO_MAX_TIMEOUT,
00069            10000 /* 10 secs */ ) != LDAP_SUCCESS ) {
00070               ldap_perror( NULL,
00071                   "prldap_set_session_option PRLDAP_OPT_IO_MAX_TIMEOUT" );
00072               exit( 1 );
00073        }
00074 
00075        /* get a handle to an LDAP connection */
00076        if ( (ld = ldapssl_init( MY_HOST, MY_SSL_PORT, 1 )) == NULL ) {
00077               perror( "ldapssl_init" );
00078               return( 1 );
00079        }
00080 
00081        /* use LDAPv3 */
00082        i = LDAP_VERSION3;
00083        if ( ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &i ) < 0 ) {
00084               ldap_perror( ld, "ldap_set_option LDAPv3" );
00085               ldap_unbind( ld );
00086               return( 1 );
00087        }
00088 
00089        /* search for all entries with surname of Jensen */
00090        if ( ldap_search_s( ld, PEOPLE_BASE, LDAP_SCOPE_SUBTREE,
00091               "(sn=jensen)", NULL, 0, &result ) != LDAP_SUCCESS ) {
00092               ldap_perror( ld, "ldap_search_s" );
00093               if ( result == NULL ) {
00094                      ldap_unbind( ld );
00095                      return( 1 );
00096               }
00097        }
00098        /* for each entry print out name + all attrs and values */
00099        for ( e = ldap_first_entry( ld, result ); e != NULL;
00100            e = ldap_next_entry( ld, e ) ) {
00101               if ( (dn = ldap_get_dn( ld, e )) != NULL ) {
00102                   printf( "dn: %s\n", dn );
00103                   ldap_memfree( dn );
00104               }
00105               for ( a = ldap_first_attribute( ld, e, &ber );
00106                   a != NULL; a = ldap_next_attribute( ld, e, ber ) ) {
00107                      if ((vals = ldap_get_values( ld, e, a)) != NULL ) {
00108                             for ( i = 0; vals[i] != NULL; i++ ) {
00109                                 printf( "%s: %s\n", a, vals[i] );
00110                             }
00111                             ldap_value_free( vals );
00112                      }
00113                      ldap_memfree( a );
00114               }
00115               if ( ber != NULL ) {
00116                      ber_free( ber, 0 );
00117               }
00118               printf( "\n" );
00119        }
00120        ldap_msgfree( result );
00121        ldap_unbind( ld );
00122        return( 0 );
00123 }