3 * Copyright 2000-2013 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
11 #include "LDAPBindRequest.h"
12 #include "LDAPException.h"
13 #include "SaslInteractionHandler.h"
14 #include "SaslInteraction.h"
17 #include <sasl/sasl.h>
21 LDAPBindRequest::LDAPBindRequest(const LDAPBindRequest& req) :
23 DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPBindRequest::LDAPBindRequest(&)" << endl);
29 LDAPBindRequest::LDAPBindRequest(const string& dn,const string& passwd,
30 LDAPAsynConnection *connect, const LDAPConstraints *cons,
31 bool isReferral) : LDAPRequest(connect, cons, isReferral){
32 DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPBindRequest::LDAPBindRequest()" << endl);
33 DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER, " dn:" << dn << endl
34 << " passwd:" << passwd << endl);
40 LDAPBindRequest::~LDAPBindRequest(){
41 DEBUG(LDAP_DEBUG_DESTROY,"LDAPBindRequest::~LDAPBindRequest()" << endl);
44 LDAPMessageQueue* LDAPBindRequest::sendRequest(){
45 DEBUG(LDAP_DEBUG_TRACE,"LDAPBindRequest::sendRequest()" << endl);
48 const char* mech = (m_mech == "" ? 0 : m_mech.c_str());
51 char* tmppwd = (char*) malloc( (m_cred.size()+1) * sizeof(char));
52 m_cred.copy(tmppwd,string::npos);
53 tmppwd[m_cred.size()]=0;
54 tmpcred=ber_bvstr(tmppwd);
56 tmpcred=(BerValue*) malloc(sizeof(BerValue));
64 LDAPControl** tmpSrvCtrls=m_cons->getSrvCtrlsArray();
65 LDAPControl** tmpClCtrls=m_cons->getClCtrlsArray();
66 int err=ldap_sasl_bind(m_connection->getSessionHandle(),dn,
67 mech, tmpcred, tmpSrvCtrls, tmpClCtrls, &msgID);
68 LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
69 LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
72 if(err != LDAP_SUCCESS){
73 throw LDAPException(err);
76 return new LDAPMessageQueue(this);
80 LDAPSaslBindRequest::LDAPSaslBindRequest(const std::string& mech,
81 const std::string& cred,
82 LDAPAsynConnection *connect,
83 const LDAPConstraints *cons,
84 bool isReferral) : LDAPRequest(connect, cons, isReferral),m_mech(mech), m_cred(cred) {}
86 LDAPMessageQueue* LDAPSaslBindRequest::sendRequest()
88 DEBUG(LDAP_DEBUG_TRACE,"LDAPSaslBindRequest::sendRequest()" << endl);
92 tmpcred.bv_val = (char*) malloc( m_cred.size() * sizeof(char));
93 m_cred.copy(tmpcred.bv_val,string::npos);
94 tmpcred.bv_len = m_cred.size();
96 LDAPControl** tmpSrvCtrls=m_cons->getSrvCtrlsArray();
97 LDAPControl** tmpClCtrls=m_cons->getClCtrlsArray();
98 int err=ldap_sasl_bind(m_connection->getSessionHandle(), "", m_mech.c_str(),
99 &tmpcred, tmpSrvCtrls, tmpClCtrls, &msgID);
100 LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
101 LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
102 free(tmpcred.bv_val);
104 if(err != LDAP_SUCCESS){
105 throw LDAPException(err);
108 return new LDAPMessageQueue(this);
112 LDAPSaslBindRequest::~LDAPSaslBindRequest()
114 DEBUG(LDAP_DEBUG_DESTROY,"LDAPSaslBindRequest::~LDAPSaslBindRequest()" << endl);
117 LDAPSaslInteractiveBind::LDAPSaslInteractiveBind( const std::string& mech,
118 int flags, SaslInteractionHandler *sih, LDAPAsynConnection *connect,
119 const LDAPConstraints *cons, bool isReferral) :
120 LDAPRequest(connect, cons, isReferral),
121 m_mech(mech), m_flags(flags), m_sih(sih), m_res(0)
125 static int my_sasl_interact(LDAP *l, unsigned flags, void *cbh, void *interact)
127 DEBUG(LDAP_DEBUG_TRACE, "LDAPSaslInteractiveBind::my_sasl_interact()"
129 std::list<SaslInteraction*> interactions;
131 sasl_interact_t *iter = (sasl_interact_t*) interact;
132 while ( iter->id != SASL_CB_LIST_END ) {
133 SaslInteraction *si = new SaslInteraction(iter);
134 interactions.push_back( si );
137 ((SaslInteractionHandler*)cbh)->handleInteractions(interactions);
141 /* This kind of fakes an asynchronous operation, ldap_sasl_interactive_bind_s
143 LDAPMessageQueue *LDAPSaslInteractiveBind::sendRequest()
145 DEBUG(LDAP_DEBUG_TRACE, "LDAPSaslInteractiveBind::sendRequest()" <<
146 m_mech << std::endl);
148 LDAPControl** tmpSrvCtrls=m_cons->getSrvCtrlsArray();
149 LDAPControl** tmpClCtrls=m_cons->getClCtrlsArray();
150 int res = ldap_sasl_interactive_bind_s( m_connection->getSessionHandle(),
151 "", m_mech.c_str(), tmpSrvCtrls, tmpClCtrls, m_flags,
152 my_sasl_interact, m_sih );
154 DEBUG(LDAP_DEBUG_TRACE, "ldap_sasl_interactive_bind_s returned: "
155 << res << std::endl);
156 if(res != LDAP_SUCCESS){
157 throw LDAPException(res);
159 m_res = new LDAPResult(LDAPMsg::BIND_RESPONSE, res, "");
161 return new LDAPMessageQueue(this);
164 LDAPMsg* LDAPSaslInteractiveBind::getNextMessage() const
169 LDAPSaslInteractiveBind::~LDAPSaslInteractiveBind()
171 DEBUG(LDAP_DEBUG_DESTROY,"LDAPSaslInteractiveBind::~LDAPSaslInteractiveBind()" << endl);