1 // $OpenLDAP$
2 /*
3 * Copyright 2000-2021 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5 */
6
7
8 #include "debug.h"
9 #include"LDAPResult.h"
10 #include"LDAPAsynConnection.h"
11 #include "LDAPRequest.h"
12 #include "LDAPException.h"
13
14 #include <cstdlib>
15
16 using namespace std;
17
LDAPResult(const LDAPRequest * req,LDAPMessage * msg)18 LDAPResult::LDAPResult(const LDAPRequest *req, LDAPMessage *msg) :
19 LDAPMsg(msg){
20 if(msg != 0){
21 DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPResult::LDAPResult()" << endl);
22 const LDAPAsynConnection *con=req->getConnection();
23 char **refs=0;
24 LDAPControl** srvctrls=0;
25 char* matchedDN=0;
26 char* errMsg=0;
27 int err=ldap_parse_result(con->getSessionHandle(),msg,&m_resCode,
28 &matchedDN, &errMsg,&refs,&srvctrls,0);
29 if(err != LDAP_SUCCESS){
30 ber_memvfree((void**) refs);
31 ldap_controls_free(srvctrls);
32 throw LDAPException(err);
33 }else{
34 if (refs){
35 m_referrals=LDAPUrlList(refs);
36 ber_memvfree((void**) refs);
37 }
38 if (srvctrls){
39 m_srvControls = LDAPControlSet(srvctrls);
40 m_hasControls = true;
41 ldap_controls_free(srvctrls);
42 }else{
43 m_hasControls = false;
44 }
45 if(matchedDN != 0){
46 m_matchedDN=string(matchedDN);
47 free(matchedDN);
48 }
49 if(errMsg != 0){
50 m_errMsg=string(errMsg);
51 free(errMsg);
52 }
53 }
54 }
55 }
56
LDAPResult(int type,int resultCode,const std::string & msg)57 LDAPResult::LDAPResult(int type, int resultCode, const std::string &msg) :
58 LDAPMsg(type,0), m_resCode(resultCode), m_errMsg(msg)
59 {}
60
61
~LDAPResult()62 LDAPResult::~LDAPResult(){
63 DEBUG(LDAP_DEBUG_DESTROY,"LDAPResult::~LDAPResult()" << endl);
64 }
65
getResultCode() const66 int LDAPResult::getResultCode() const{
67 DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getResultCode()" << endl);
68 return m_resCode;
69 }
70
resToString() const71 string LDAPResult::resToString() const{
72 DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::resToString()" << endl);
73 return string(ldap_err2string(m_resCode));
74 }
75
getErrMsg() const76 const string& LDAPResult::getErrMsg() const{
77 DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getErrMsg()" << endl);
78 return m_errMsg;
79 }
80
getMatchedDN() const81 const string& LDAPResult::getMatchedDN() const{
82 DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getMatchedDN()" << endl);
83 return m_matchedDN;
84 }
85
getReferralUrls() const86 const LDAPUrlList& LDAPResult::getReferralUrls() const{
87 DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getReferralUrl()" << endl);
88 return m_referrals;
89 }
90
operator <<(ostream & s,LDAPResult & l)91 ostream& operator<<(ostream &s,LDAPResult &l){
92 return s << "Result: " << l.m_resCode << ": "
93 << ldap_err2string(l.m_resCode) << endl
94 << "Matched: " << l.m_matchedDN << endl << "ErrMsg: " << l.m_errMsg;
95 }
96
97