LDAPResult.cpp 2.7 KB
Newer Older
Ralf Haferkamp's avatar
Ralf Haferkamp committed
1
// $OpenLDAP$
2
/*
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
3
 * Copyright 2000-2021 The OpenLDAP Foundation, All Rights Reserved.
4
5
6
7
8
9
10
11
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */


#include "debug.h"
#include"LDAPResult.h"
#include"LDAPAsynConnection.h"
#include "LDAPRequest.h"
12
#include "LDAPException.h"
13

Ralf Haferkamp's avatar
Ralf Haferkamp committed
14
15
#include <cstdlib>

16
17
using namespace std;

18
19
20
21
LDAPResult::LDAPResult(const LDAPRequest *req, LDAPMessage *msg) : 
        LDAPMsg(msg){
    if(msg != 0){
        DEBUG(LDAP_DEBUG_CONSTRUCT,"LDAPResult::LDAPResult()" << endl);
22
23
        const LDAPAsynConnection *con=req->getConnection();
        char **refs=0;
24
25
26
27
28
29
        LDAPControl** srvctrls=0;
        char* matchedDN=0;
        char* errMsg=0;
        int err=ldap_parse_result(con->getSessionHandle(),msg,&m_resCode,
                &matchedDN, &errMsg,&refs,&srvctrls,0);
        if(err != LDAP_SUCCESS){
30
            ber_memvfree((void**) refs);
31
32
33
34
35
            ldap_controls_free(srvctrls);
            throw LDAPException(err);
        }else{
            if (refs){
                m_referrals=LDAPUrlList(refs);
36
                ber_memvfree((void**) refs);
37
38
39
40
41
42
43
44
45
46
            }
            if (srvctrls){
                m_srvControls = LDAPControlSet(srvctrls);
                m_hasControls = true;
                ldap_controls_free(srvctrls);
            }else{
                m_hasControls = false;
            }
            if(matchedDN != 0){
                m_matchedDN=string(matchedDN);
47
                free(matchedDN);
48
49
50
            }
            if(errMsg != 0){
                m_errMsg=string(errMsg);
51
                free(errMsg);
52
53
            }
        }
54
    }
55
56
}

Ralf Haferkamp's avatar
Ralf Haferkamp committed
57
58
59
60
61
LDAPResult::LDAPResult(int type, int resultCode, const std::string &msg) : 
        LDAPMsg(type,0), m_resCode(resultCode), m_errMsg(msg)
{}


62
LDAPResult::~LDAPResult(){
63
    DEBUG(LDAP_DEBUG_DESTROY,"LDAPResult::~LDAPResult()" << endl);
64
65
}

66
67
int LDAPResult::getResultCode() const{
    DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getResultCode()" << endl);
68
69
70
    return m_resCode;
}

71
72
73
74
75
76
77
78
string LDAPResult::resToString() const{
    DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::resToString()" << endl);
    return string(ldap_err2string(m_resCode));
}

const string& LDAPResult::getErrMsg() const{
    DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getErrMsg()" << endl);
    return m_errMsg;
79
80
}

81
82
83
const string& LDAPResult::getMatchedDN() const{
    DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getMatchedDN()" << endl);
    return m_matchedDN;
84
85
}

86
87
88
const LDAPUrlList& LDAPResult::getReferralUrls() const{
    DEBUG(LDAP_DEBUG_TRACE,"LDAPResult::getReferralUrl()" << endl);
    return m_referrals;
89
90
91
}

ostream& operator<<(ostream &s,LDAPResult &l){
92
    return s << "Result: " << l.m_resCode << ": "  
93
94
95
96
        << ldap_err2string(l.m_resCode) << endl 
        << "Matched: " << l.m_matchedDN << endl << "ErrMsg: " << l.m_errMsg;
}