Back to index

openldap  2.4.31
startTls.cpp
Go to the documentation of this file.
00001 // $OpenLDAP$
00002 /*
00003  * Copyright 2010-2012 The OpenLDAP Foundation, All Rights Reserved.
00004  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
00005  */
00006 
00007 #include <iostream>
00008 #include <string>
00009 #include "LDAPAsynConnection.h"
00010 #include "TlsOptions.h"
00011 
00012 int main( int argc, char* argv[]){
00013     if ( argc != 4 ){
00014         std::cerr << "usage: " << argv[0] << " <ldap-uri> <cacertfile> <cacertdir>" << std::endl;
00015         return(-1);
00016     }
00017     std::string uri(argv[1]);
00018     std::string cacert(argv[2]);
00019     std::string cadir(argv[3]);
00020     TlsOptions tls;
00021     std::cout << "Current global settings:" << std::endl;
00022     std::cout << "    CaCertfile: " << tls.getStringOption( TlsOptions::CACERTFILE) << std::endl;
00023     std::cout << "    CaCertDir: " << tls.getStringOption( TlsOptions::CACERTDIR ) << std::endl;
00024     std::cout << "    Require Cert: " << tls.getIntOption( TlsOptions::REQUIRE_CERT ) << std::endl;
00025     std::cout << "Applying new settings:" << std::endl;
00026     tls.setOption( TlsOptions::CACERTFILE, cacert );
00027     tls.setOption( TlsOptions::REQUIRE_CERT, TlsOptions::DEMAND );
00028     std::cout << "    CaCertfile: " << tls.getStringOption( TlsOptions::CACERTFILE ) << std::endl;
00029     std::cout << "    Require Cert: " << tls.getIntOption( TlsOptions::REQUIRE_CERT ) << std::endl;
00030 
00031     try {
00032         // 1. connect using global options
00033         LDAPAsynConnection l(uri);
00034         try {
00035             l.start_tls();
00036             std::cout << "StartTLS successful." << std::endl;
00037             l.unbind();
00038         } catch ( LDAPException e ) {
00039             std::cerr << e << std::endl;
00040         }
00041 
00042         // 2. connect using connection specific option
00043         LDAPAsynConnection l1(uri);
00044         tls=l1.getTlsOptions();
00045         std::cout << "Current connection specific settings:" << std::endl;
00046         std::cout << "    CaCertfile: " << tls.getStringOption( TlsOptions::CACERTFILE) << std::endl;
00047         std::cout << "    CaCertDir: " << tls.getStringOption( TlsOptions::CACERTDIR ) << std::endl;
00048         std::cout << "    Require Cert: " << tls.getIntOption( TlsOptions::REQUIRE_CERT ) << std::endl;
00049         std::cout << "Applying new settings:" << std::endl;
00050         tls.setOption( TlsOptions::CACERTDIR, cadir );
00051         tls.setOption( TlsOptions::REQUIRE_CERT, TlsOptions::DEMAND );
00052         std::cout << "    CaCertDir: " << tls.getStringOption( TlsOptions::CACERTDIR ) << std::endl;
00053         std::cout << "    Require Cert: " << tls.getIntOption( TlsOptions::REQUIRE_CERT ) << std::endl;
00054         try {
00055             l1.start_tls();
00056             std::cout << "StartTLS successful." << std::endl;
00057             l1.unbind();
00058         } catch ( LDAPException e ) {
00059             std::cerr << e << std::endl;
00060         }
00061 
00062         // 3. and once again using the globals
00063         try {
00064             LDAPAsynConnection l2(uri);
00065             TlsOptions tls2;
00066             std::cout << "Current global settings:" << std::endl;
00067             std::cout << "    CaCertfile: " << tls2.getStringOption( TlsOptions::CACERTFILE) << std::endl;
00068             std::cout << "    CaCertDir: " << tls2.getStringOption( TlsOptions::CACERTDIR ) << std::endl;
00069             std::cout << "    Require Cert: " << tls2.getIntOption( TlsOptions::REQUIRE_CERT ) << std::endl;
00070             l2.start_tls();
00071             std::cout << "StartTLS successful." << std::endl;
00072             l2.unbind();
00073         } catch ( LDAPException e ) {
00074             std::cerr << e << std::endl;
00075         }
00076     } catch ( LDAPException e ) {
00077         std::cerr << e << std::endl;
00078     }
00079 }