First commit
[anna.git] / source / ldap / Search.cpp
1 // ANNA - Anna is Not 'N' 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 #include <anna/core/tracing/Logger.hpp>
42
43 #include <anna/xml/Node.hpp>
44
45 #include <anna/ldap/Search.hpp>
46 #include <anna/ldap/Session.hpp>
47
48 using namespace std;
49 using namespace anna;
50 using namespace anna::ldap;
51
52 Search::attribute_pool Search::st_attributes;
53
54 void Search::clear()
55 throw() {
56   a_base.clear();
57   a_scope = Scope::Base;
58   clearFilter();
59   a_onlyType = false,
60   clearSizeLimit();
61   clearAttributes();
62 }
63
64 void Search::clearAttributes()
65 throw() {
66   for(attribute_iterator ii = attribute_begin(), maxii = attribute_end(); ii != maxii; ii ++)
67     st_attributes.release(&attribute(ii));
68
69   a_attributes.clear();
70 }
71
72 IdMessage Search::send(Session& session) const
73 throw() {
74   IdMessage result(-1);
75   LDAP* ldap = (LDAP*) session.getLDAP();
76   int scope = -1;
77
78   switch(a_scope) {
79   case Scope::Base: scope = LDAP_SCOPE_BASE; break;
80   case Scope::OneLevel: scope = LDAP_SCOPE_ONELEVEL; break;
81   case Scope::SubTree: scope = LDAP_SCOPE_SUBTREE; break;
82   }
83
84   int maxi = attribute_size() + 1;
85   char** attributes = NULL;
86
87   if(maxi > 1) {
88     attributes = new char* [maxi];
89     int i = 0;
90
91     for(const_attribute_iterator ii = attribute_begin(), maxii = attribute_end(); ii != maxii; ii ++)
92       attributes [i ++] = const_cast <char*>(attribute(ii).c_str());
93
94     attributes [i] = NULL;
95   }
96
97   result = ldap_search(ldap, a_base.c_str(), scope, asCString(a_filter), attributes, a_onlyType);
98   LOGDEBUG(
99     string msg("ldap::Search::send | Scope: ");
100     msg += functions::asString(scope);
101     msg += " | ";
102     msg += asString();
103     msg += " | Attributes: ";
104
105   if(attributes != NULL) {
106   for(int ii = 0; attributes [ii] != NULL; ii ++) {
107       msg += attributes [ii];
108       msg += ' ';
109     }
110   } else
111     msg += "<null>";
112     Logger::debug(msg, ANNA_FILE_LOCATION);
113     );
114
115   delete [] attributes;
116   return result;
117 }
118
119 string Search::asString() const
120 throw() {
121   string result("ldap::Search { ");
122   result += Request::asString();
123   result += " | Base: ";
124   result += a_base;
125   result += " | Scope: ";
126   result += Scope::asText(a_scope);
127   result += " | Filter: ";
128   result += asText(a_filter);
129   result += functions::asText(" | OnlyType: ", a_onlyType);
130   result += functions::asText(" | SizeLimit: ", a_sizeLimit);
131   return result += " }";
132 }
133
134 xml::Node* Search::asXML(xml::Node* parent) const
135 throw() {
136   parent = Request::asXML(parent);
137   parent->createAttribute("Base", a_base);
138   parent->createAttribute("Scope", Scope::asText(a_scope));
139   parent->createAttribute("Filter", asText(a_filter));
140   parent->createAttribute("OnlyType", functions::asString(a_onlyType));
141   parent->createAttribute("SizeLimit", a_sizeLimit);
142   return parent;
143 }
144