Back to index

openldap  2.4.31
LDAPBindRequest.cpp
Go to the documentation of this file.
00001 // $OpenLDAP$
00002 /*
00003  * Copyright 2000-2012 The OpenLDAP Foundation, All Rights Reserved.
00004  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
00005  */
00006 
00007 #include <ldap.h>
00008 
00009 #include "debug.h"
00010 
00011 #include "LDAPBindRequest.h"
00012 #include "LDAPException.h"
00013 #include "SaslInteractionHandler.h"
00014 #include "SaslInteraction.h"
00015 
00016 #include <cstdlib>
00017 #include <sasl/sasl.h>
00018 
00019 using namespace std;
00020 
00021 LDAPBindRequest::LDAPBindRequest(const LDAPBindRequest& req) :
00022         LDAPRequest(req){
00023     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPBindRequest::LDAPBindRequest(&)" << endl);
00024     m_dn=req.m_dn;
00025     m_cred=req.m_cred;
00026     m_mech=req.m_mech;
00027 }
00028 
00029 LDAPBindRequest::LDAPBindRequest(const string& dn,const string& passwd, 
00030         LDAPAsynConnection *connect, const LDAPConstraints *cons,
00031         bool isReferral) : LDAPRequest(connect, cons, isReferral){
00032    DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPBindRequest::LDAPBindRequest()" << endl);
00033    DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER, "   dn:" << dn << endl
00034            << "   passwd:" << passwd << endl);
00035     m_dn = dn;
00036     m_cred = passwd;
00037     m_mech = "";
00038 }
00039 
00040 LDAPBindRequest::~LDAPBindRequest(){
00041     DEBUG(LDAP_DEBUG_DESTROY,"LDAPBindRequest::~LDAPBindRequest()" << endl);
00042 }
00043 
00044 LDAPMessageQueue* LDAPBindRequest::sendRequest(){
00045     DEBUG(LDAP_DEBUG_TRACE,"LDAPBindRequest::sendRequest()" << endl);
00046     int msgID=0;
00047     
00048     const char* mech = (m_mech == "" ? 0 : m_mech.c_str());
00049     BerValue* tmpcred=0;
00050     if(m_cred != ""){
00051         char* tmppwd = (char*) malloc( (m_cred.size()+1) * sizeof(char));
00052         m_cred.copy(tmppwd,string::npos);
00053         tmppwd[m_cred.size()]=0;
00054         tmpcred=ber_bvstr(tmppwd);
00055     }else{
00056         tmpcred=(BerValue*) malloc(sizeof(BerValue));
00057         tmpcred->bv_len=0;
00058         tmpcred->bv_val=0;
00059     }
00060     const char* dn = 0;
00061     if(m_dn != ""){
00062         dn = m_dn.c_str();
00063     }
00064     LDAPControl** tmpSrvCtrls=m_cons->getSrvCtrlsArray();
00065     LDAPControl** tmpClCtrls=m_cons->getClCtrlsArray();
00066     int err=ldap_sasl_bind(m_connection->getSessionHandle(),dn, 
00067             mech, tmpcred, tmpSrvCtrls, tmpClCtrls, &msgID);
00068     LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
00069     LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
00070     ber_bvfree(tmpcred);
00071 
00072     if(err != LDAP_SUCCESS){
00073         throw LDAPException(err);
00074     }else{
00075         m_msgID=msgID;
00076         return new LDAPMessageQueue(this);
00077     }
00078 }
00079 
00080 LDAPSaslBindRequest::LDAPSaslBindRequest(const std::string& mech,
00081         const std::string& cred, 
00082         LDAPAsynConnection *connect,
00083         const LDAPConstraints *cons, 
00084         bool isReferral) : LDAPRequest(connect, cons, isReferral),m_mech(mech), m_cred(cred) {}
00085 
00086 LDAPMessageQueue* LDAPSaslBindRequest::sendRequest()
00087 {
00088     DEBUG(LDAP_DEBUG_TRACE,"LDAPSaslBindRequest::sendRequest()" << endl);
00089     int msgID=0;
00090     
00091     BerValue tmpcred;
00092     tmpcred.bv_val = (char*) malloc( m_cred.size() * sizeof(char));
00093     m_cred.copy(tmpcred.bv_val,string::npos);
00094     tmpcred.bv_len = m_cred.size();
00095     
00096     LDAPControl** tmpSrvCtrls=m_cons->getSrvCtrlsArray();
00097     LDAPControl** tmpClCtrls=m_cons->getClCtrlsArray();
00098     int err=ldap_sasl_bind(m_connection->getSessionHandle(), "", m_mech.c_str(), 
00099             &tmpcred, tmpSrvCtrls, tmpClCtrls, &msgID);
00100     LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
00101     LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
00102     free(tmpcred.bv_val);
00103 
00104     if(err != LDAP_SUCCESS){
00105         throw LDAPException(err);
00106     }else{
00107         m_msgID=msgID;
00108         return new LDAPMessageQueue(this);
00109     }
00110 }
00111 
00112 LDAPSaslBindRequest::~LDAPSaslBindRequest()
00113 {
00114     DEBUG(LDAP_DEBUG_DESTROY,"LDAPSaslBindRequest::~LDAPSaslBindRequest()" << endl);
00115 }
00116 
00117 LDAPSaslInteractiveBind::LDAPSaslInteractiveBind( const std::string& mech, 
00118         int flags, SaslInteractionHandler *sih, LDAPAsynConnection *connect,
00119         const LDAPConstraints *cons, bool isReferral) : 
00120             LDAPRequest(connect, cons, isReferral),
00121             m_mech(mech), m_flags(flags), m_sih(sih), m_res(0)
00122 {
00123 }
00124 
00125 static int my_sasl_interact(LDAP *l, unsigned flags, void *cbh, void *interact)
00126 {
00127     DEBUG(LDAP_DEBUG_TRACE, "LDAPSaslInteractiveBind::my_sasl_interact()" 
00128             << std::endl );
00129     std::list<SaslInteraction*> interactions;
00130 
00131     sasl_interact_t *iter = (sasl_interact_t*) interact;
00132     while ( iter->id != SASL_CB_LIST_END ) {
00133         SaslInteraction *si = new SaslInteraction(iter);
00134         interactions.push_back( si );
00135         iter++;
00136     }
00137     ((SaslInteractionHandler*)cbh)->handleInteractions(interactions);
00138     return LDAP_SUCCESS;
00139 }
00140 
00141 /* This kind of fakes an asynchronous operation, ldap_sasl_interactive_bind_s
00142  * is synchronous */
00143 LDAPMessageQueue *LDAPSaslInteractiveBind::sendRequest()
00144 {
00145     DEBUG(LDAP_DEBUG_TRACE, "LDAPSaslInteractiveBind::sendRequest()" <<
00146             m_mech << std::endl);
00147 
00148     LDAPControl** tmpSrvCtrls=m_cons->getSrvCtrlsArray();
00149     LDAPControl** tmpClCtrls=m_cons->getClCtrlsArray();
00150     int res = ldap_sasl_interactive_bind_s( m_connection->getSessionHandle(),
00151             "", m_mech.c_str(), tmpSrvCtrls, tmpClCtrls, m_flags, 
00152             my_sasl_interact, m_sih );
00153 
00154     DEBUG(LDAP_DEBUG_TRACE, "ldap_sasl_interactive_bind_s returned: " 
00155             << res << std::endl);
00156     if(res != LDAP_SUCCESS){
00157         throw LDAPException(res);
00158     } else {
00159         m_res = new LDAPResult(LDAPMsg::BIND_RESPONSE, res, ""); 
00160     }
00161     return new LDAPMessageQueue(this);
00162 }
00163 
00164 LDAPMsg* LDAPSaslInteractiveBind::getNextMessage() const 
00165 {
00166     return m_res;
00167 }
00168 
00169 LDAPSaslInteractiveBind::~LDAPSaslInteractiveBind()
00170 {
00171     DEBUG(LDAP_DEBUG_DESTROY,"LDAPSaslInteractiveBind::~LDAPSaslInteractiveBind()" << endl);
00172 }
00173