1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
40 #include <anna/core/functions.hpp>
42 #include <anna/ldap/ResultCode.hpp>
43 #include <anna/ldap/Session.hpp>
47 #define DoErrorCode(methodName,macroName) \
48 bool anna::ldap::ResultCode::methodName () const \
51 return a_value == (macroName); \
54 DoErrorCode(isOperationsError, LDAP_OPERATIONS_ERROR)
55 DoErrorCode(isInvalidCredential, LDAP_INVALID_CREDENTIALS)
56 DoErrorCode(isProtocolError, LDAP_PROTOCOL_ERROR)
57 DoErrorCode(isTimeLimitExceeded, LDAP_TIMELIMIT_EXCEEDED)
58 DoErrorCode(isSizeLimitExceeded, LDAP_SIZELIMIT_EXCEEDED)
59 DoErrorCode(isAuthMethodNotSupported, LDAP_AUTH_METHOD_NOT_SUPPORTED)
60 DoErrorCode(isStrongAuthRequired, LDAP_STRONG_AUTH_REQUIRED)
61 DoErrorCode(isSASLBindInProgress, LDAP_SASL_BIND_IN_PROGRESS)
62 DoErrorCode(isTimeout, LDAP_TIMEOUT)
63 DoErrorCode(isUnavailable, LDAP_UNAVAILABLE)
64 DoErrorCode(isServerDown, LDAP_SERVER_DOWN)
65 DoErrorCode(isLocalError, LDAP_LOCAL_ERROR)
66 DoErrorCode(isDecodingError, LDAP_DECODING_ERROR)
67 DoErrorCode(isFilterError, LDAP_FILTER_ERROR)
68 DoErrorCode(isConnectError, LDAP_CONNECT_ERROR)
70 #define DoErrorRange(methodName,rangeName) \
71 bool anna::ldap::ResultCode::methodName () const \
74 return rangeName (a_value) != 0; \
77 DoErrorRange(isAttrError, LDAP_ATTR_ERROR)
78 DoErrorRange(isNameError, LDAP_NAME_ERROR)
79 DoErrorRange(isSecurityError, LDAP_SECURITY_ERROR)
80 DoErrorRange(isServiceError, LDAP_SERVICE_ERROR)
82 anna::ldap::ResultCode::ResultCode(const int ldap_method_result) :
83 a_value(ldap_method_result) {
84 if(ldap_method_result != LDAP_SUCCESS) {
85 const char* text = ldap_err2string(ldap_method_result);
86 a_text = (text == NULL) ? "<undefined>" : text;
92 * Al invocar a un método que devuelva el resultado y además haya que pasar una variable
93 * donde contener el error pasaremos ldap::ResultCode::extraError, para que vuelve ahí el
95 + Una vez hecho eso, el código de error "real" será el máximo de los dos. Por es posible
96 * que el método invicado devuelva 0, pero indique el código de error el extraError.
98 anna::ldap::ResultCode& anna::ldap::ResultCode::operator= (const int ldap_method_result)
100 if((a_value = ldap_method_result) != LDAP_SUCCESS) {
101 const char* text = ldap_err2string(ldap_method_result);
102 a_text = (text == NULL) ? "<undefined>" : text;
108 void anna::ldap::ResultCode::setValue(const int ldap_method_result, const int ldap_method_error)
110 if(ldap_method_result != LDAP_SUCCESS)
111 operator= (ldap_method_result);
113 operator= (ldap_method_error);
116 const string anna::ldap::ResultCode::asString() const
118 string result(functions::asText("ldap::ResultCode { Value: ", a_value));
120 if(a_text.empty() == false) {
121 result += " | Text: ";
125 return result += " }";
128 bool anna::ldap::ResultCode::extractResultCode(const Session* session)
130 LDAP* handle = (LDAP*)((Session*) session)->getLDAP();
133 if(ldap_get_option(handle, LDAP_OPT_RESULT_CODE, &result) == LDAP_OPT_SUCCESS) {