Updated license
[anna.git] / source / ldap / ResultCode.cpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
17 //     * Neither the name of Google Inc. 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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <time.h>
38 #include <ldap.h>
39
40 #include <anna/core/functions.hpp>
41
42 #include <anna/ldap/ResultCode.hpp>
43 #include <anna/ldap/Session.hpp>
44
45 using namespace std;
46
47 #define DoErrorCode(methodName,macroName) \
48    bool anna::ldap::ResultCode::methodName () const \
49       throw () \
50    { \
51       return a_value == (macroName); \
52    }
53
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)
69
70 #define DoErrorRange(methodName,rangeName) \
71    bool anna::ldap::ResultCode::methodName () const \
72       throw () \
73    { \
74       return rangeName (a_value) != 0; \
75    }
76
77 DoErrorRange(isAttrError, LDAP_ATTR_ERROR)
78 DoErrorRange(isNameError, LDAP_NAME_ERROR)
79 DoErrorRange(isSecurityError, LDAP_SECURITY_ERROR)
80 DoErrorRange(isServiceError, LDAP_SERVICE_ERROR)
81
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;
87   }
88 }
89
90
91 /**
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
94  * código de error.
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.
97  */
98 anna::ldap::ResultCode& anna::ldap::ResultCode::operator= (const int ldap_method_result)
99 throw() {
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;
103   }
104
105   return *this;
106 }
107
108 void anna::ldap::ResultCode::setValue(const int ldap_method_result, const int ldap_method_error)
109 throw() {
110   if(ldap_method_result != LDAP_SUCCESS)
111     operator= (ldap_method_result);
112   else
113     operator= (ldap_method_error);
114 }
115
116 const string  anna::ldap::ResultCode::asString() const
117 throw() {
118   string result(functions::asText("ldap::ResultCode { Value: ", a_value));
119
120   if(a_text.empty() == false) {
121     result += " | Text: ";
122     result += a_text;
123   }
124
125   return result += " }";
126 }
127
128 bool anna::ldap::ResultCode::extractResultCode(const Session* session)
129 throw() {
130   LDAP* handle = (LDAP*)((Session*) session)->getLDAP();
131   int result = -1;
132
133   if(ldap_get_option(handle, LDAP_OPT_RESULT_CODE, &result) == LDAP_OPT_SUCCESS) {
134     operator= (result);
135     return true;
136   }
137
138   return false;
139 }