2 * Copyright 2000, OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include "LDAPBindRequest.h"
11 #include "LDAPException.h"
12 #include "SaslInteractionHandler.h"
13 #include "SaslInteraction.h"
16 #include <sasl/sasl.h>
20 LDAPBindRequest::LDAPBindRequest(const LDAPBindRequest& req) :
22 DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPBindRequest::LDAPBindRequest(&)" << endl);
28 LDAPBindRequest::LDAPBindRequest(const string& dn,const string& passwd,
29 LDAPAsynConnection *connect, const LDAPConstraints *cons,
30 bool isReferral) : LDAPRequest(connect, cons, isReferral){
31 DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPBindRequest::LDAPBindRequest()" << endl);
32 DEBUG(LDAP_DEBUG_CONSTRUCT | LDAP_DEBUG_PARAMETER, " dn:" << dn << endl
33 << " passwd:" << passwd << endl);
39 LDAPBindRequest::~LDAPBindRequest(){
40 DEBUG(LDAP_DEBUG_DESTROY,"LDAPBindRequest::~LDAPBindRequest()" << endl);
43 LDAPMessageQueue* LDAPBindRequest::sendRequest(){
44 DEBUG(LDAP_DEBUG_TRACE,"LDAPBindRequest::sendRequest()" << endl);
47 const char* mech = (m_mech == "" ? 0 : m_mech.c_str());
50 char* tmppwd = (char*) malloc( (m_cred.size()+1) * sizeof(char));
51 m_cred.copy(tmppwd,string::npos);
52 tmppwd[m_cred.size()]=0;
53 tmpcred=ber_bvstr(tmppwd);
55 tmpcred=(BerValue*) malloc(sizeof(BerValue));
63 LDAPControl** tmpSrvCtrls=m_cons->getSrvCtrlsArray();
64 LDAPControl** tmpClCtrls=m_cons->getClCtrlsArray();
65 int err=ldap_sasl_bind(m_connection->getSessionHandle(),dn,
66 mech, tmpcred, tmpSrvCtrls, tmpClCtrls, &msgID);
67 LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
68 LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
71 if(err != LDAP_SUCCESS){
72 throw LDAPException(err);
75 return new LDAPMessageQueue(this);
79 LDAPSaslBindRequest::LDAPSaslBindRequest(const std::string& mech,
80 const std::string& cred,
81 LDAPAsynConnection *connect,
82 const LDAPConstraints *cons,
83 bool isReferral) : LDAPRequest(connect, cons, isReferral),m_mech(mech), m_cred(cred) {}
85 LDAPMessageQueue* LDAPSaslBindRequest::sendRequest()
87 DEBUG(LDAP_DEBUG_TRACE,"LDAPSaslBindRequest::sendRequest()" << endl);
91 tmpcred.bv_val = (char*) malloc( m_cred.size() * sizeof(char));
92 m_cred.copy(tmpcred.bv_val,string::npos);
93 tmpcred.bv_len = m_cred.size();
95 LDAPControl** tmpSrvCtrls=m_cons->getSrvCtrlsArray();
96 LDAPControl** tmpClCtrls=m_cons->getClCtrlsArray();
97 int err=ldap_sasl_bind(m_connection->getSessionHandle(), "", m_mech.c_str(),
98 &tmpcred, tmpSrvCtrls, tmpClCtrls, &msgID);
99 LDAPControlSet::freeLDAPControlArray(tmpSrvCtrls);
100 LDAPControlSet::freeLDAPControlArray(tmpClCtrls);
101 free(tmpcred.bv_val);
103 if(err != LDAP_SUCCESS){
104 throw LDAPException(err);
107 return new LDAPMessageQueue(this);
111 LDAPSaslBindRequest::~LDAPSaslBindRequest()
113 DEBUG(LDAP_DEBUG_DESTROY,"LDAPSaslBindRequest::~LDAPSaslBindRequest()" << endl);
116 LDAPSaslInteractiveBind::LDAPSaslInteractiveBind( const std::string& mech,
117 int flags, SaslInteractionHandler *sih, LDAPAsynConnection *connect,
118 const LDAPConstraints *cons, bool isReferral) :
119 LDAPRequest(connect, cons, isReferral),
120 m_mech(mech), m_flags(flags), m_sih(sih), m_res(0)
124 static int my_sasl_interact(LDAP *l, unsigned flags, void *cbh, void *interact)
126 DEBUG(LDAP_DEBUG_TRACE, "LDAPSaslInteractiveBind::my_sasl_interact()"
128 std::list<SaslInteraction*> interactions;
130 sasl_interact_t *iter = (sasl_interact_t*) interact;
131 while ( iter->id != SASL_CB_LIST_END ) {
132 SaslInteraction *si = new SaslInteraction(iter);
133 interactions.push_back( si );
136 ((SaslInteractionHandler*)cbh)->handleInteractions(interactions);
140 /* This kind of fakes an asynchronous operation, ldap_sasl_interactive_bind_s
142 LDAPMessageQueue *LDAPSaslInteractiveBind::sendRequest()
144 DEBUG(LDAP_DEBUG_TRACE, "LDAPSaslInteractiveBind::sendRequest()" <<
145 m_mech << std::endl);
147 LDAPControl** tmpSrvCtrls=m_cons->getSrvCtrlsArray();
148 LDAPControl** tmpClCtrls=m_cons->getClCtrlsArray();
149 int res = ldap_sasl_interactive_bind_s( m_connection->getSessionHandle(),
150 "", m_mech.c_str(), tmpSrvCtrls, tmpClCtrls, m_flags,
151 my_sasl_interact, m_sih );
153 DEBUG(LDAP_DEBUG_TRACE, "ldap_sasl_interactive_bind_s returned: "
154 << res << std::endl);
155 if(res != LDAP_SUCCESS){
156 throw LDAPException(res);
158 m_res = new LDAPResult(LDAPMsg::BIND_RESPONSE, res, "");
160 return new LDAPMessageQueue(this);
163 LDAPMsg* LDAPSaslInteractiveBind::getNextMessage() const
168 LDAPSaslInteractiveBind::~LDAPSaslInteractiveBind()
170 DEBUG(LDAP_DEBUG_DESTROY,"LDAPSaslInteractiveBind::~LDAPSaslInteractiveBind()" << endl);