]> git.sur5r.net Git - openldap/blob - contrib/ldapc++/src/LDAPBindRequest.cpp
initial support for SASL
[openldap] / contrib / ldapc++ / src / LDAPBindRequest.cpp
1 /*
2  * Copyright 2000-2007, OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5
6 #include <ldap.h>
7
8 #include "debug.h"
9
10 #include "LDAPBindRequest.h"
11 #include "LDAPException.h"
12 #include "SaslInteractionHandler.h"
13 #include "SaslInteraction.h"
14
15 #include <cstdlib>
16 #include <sasl/sasl.h>
17
18 using namespace std;
19
20 LDAPBindRequest::LDAPBindRequest(const LDAPBindRequest& req) :
21         LDAPRequest(req){
22     DEBUG(LDAP_DEBUG_CONSTRUCT, "LDAPBindRequest::LDAPBindRequest(&)" << endl);
23     m_dn=req.m_dn;
24     m_cred=req.m_cred;
25     m_mech=req.m_mech;
26 }
27
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);
34     m_dn = dn;
35     m_cred = passwd;
36     m_mech = "";
37 }
38
39 LDAPBindRequest::~LDAPBindRequest(){
40     DEBUG(LDAP_DEBUG_DESTROY,"LDAPBindRequest::~LDAPBindRequest()" << endl);
41 }
42
43 LDAPMessageQueue* LDAPBindRequest::sendRequest(){
44     DEBUG(LDAP_DEBUG_TRACE,"LDAPBindRequest::sendRequest()" << endl);
45     int msgID=0;
46     
47     const char* mech = (m_mech == "" ? 0 : m_mech.c_str());
48     BerValue* tmpcred=0;
49     if(m_cred != ""){
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);
54     }else{
55         tmpcred=(BerValue*) malloc(sizeof(BerValue));
56         tmpcred->bv_len=0;
57         tmpcred->bv_val=0;
58     }
59     const char* dn = 0;
60     if(m_dn != ""){
61         dn = m_dn.c_str();
62     }
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);
69     ber_bvfree(tmpcred);
70
71     if(err != LDAP_SUCCESS){
72         throw LDAPException(err);
73     }else{
74         m_msgID=msgID;
75         return new LDAPMessageQueue(this);
76     }
77 }
78
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) {}
84
85 LDAPMessageQueue* LDAPSaslBindRequest::sendRequest()
86 {
87     DEBUG(LDAP_DEBUG_TRACE,"LDAPSaslBindRequest::sendRequest()" << endl);
88     int msgID=0;
89     
90     BerValue tmpcred;
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();
94     
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);
102
103     if(err != LDAP_SUCCESS){
104         throw LDAPException(err);
105     }else{
106         m_msgID=msgID;
107         return new LDAPMessageQueue(this);
108     }
109 }
110
111 LDAPSaslBindRequest::~LDAPSaslBindRequest()
112 {
113     DEBUG(LDAP_DEBUG_DESTROY,"LDAPSaslBindRequest::~LDAPSaslBindRequest()" << endl);
114 }
115
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)
121 {
122 }
123
124 static int my_sasl_interact(LDAP *l, unsigned flags, void *cbh, void *interact)
125 {
126     DEBUG(LDAP_DEBUG_TRACE, "LDAPSaslInteractiveBind::my_sasl_interact()" 
127             << std::endl );
128     std::list<SaslInteraction*> interactions;
129
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 );
134         iter++;
135     }
136     ((SaslInteractionHandler*)cbh)->handleInteractions(interactions);
137     return LDAP_SUCCESS;
138 }
139
140 /* This kind of fakes an asynchronous operation, ldap_sasl_interactive_bind_s
141  * is synchronous */
142 LDAPMessageQueue *LDAPSaslInteractiveBind::sendRequest()
143 {
144     DEBUG(LDAP_DEBUG_TRACE, "LDAPSaslInteractiveBind::sendRequest()" <<
145             m_mech << std::endl);
146
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 );
152
153     DEBUG(LDAP_DEBUG_TRACE, "ldap_sasl_interactive_bind_s returned: " 
154             << res << std::endl);
155     if(res != LDAP_SUCCESS){
156         throw LDAPException(res);
157     } else {
158         m_res = new LDAPResult(LDAPMsg::BIND_RESPONSE, res, ""); 
159     }
160     return new LDAPMessageQueue(this);
161 }
162
163 LDAPMsg* LDAPSaslInteractiveBind::getNextMessage() const 
164 {
165     return m_res;
166 }
167
168 LDAPSaslInteractiveBind::~LDAPSaslInteractiveBind()
169 {
170     DEBUG(LDAP_DEBUG_DESTROY,"LDAPSaslInteractiveBind::~LDAPSaslInteractiveBind()" << endl);
171 }
172