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>
41 #include <anna/core/tracing/Logger.hpp>
43 #include <anna/xml/Node.hpp>
45 #include <anna/ldap/Search.hpp>
46 #include <anna/ldap/Session.hpp>
50 using namespace anna::ldap;
52 Search::attribute_pool Search::st_attributes;
57 a_scope = Scope::Base;
64 void Search::clearAttributes()
66 for(attribute_iterator ii = attribute_begin(), maxii = attribute_end(); ii != maxii; ii ++)
67 st_attributes.release(&attribute(ii));
72 IdMessage Search::send(Session& session) const
75 LDAP* ldap = (LDAP*) session.getLDAP();
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;
84 int maxi = attribute_size() + 1;
85 char** attributes = NULL;
88 attributes = new char* [maxi];
91 for(const_attribute_iterator ii = attribute_begin(), maxii = attribute_end(); ii != maxii; ii ++)
92 attributes [i ++] = const_cast <char*>(attribute(ii).c_str());
94 attributes [i] = NULL;
97 result = ldap_search(ldap, a_base.c_str(), scope, asCString(a_filter), attributes, a_onlyType);
99 string msg("ldap::Search::send | Scope: ");
100 msg += functions::asString(scope);
103 msg += " | Attributes: ";
105 if(attributes != NULL) {
106 for(int ii = 0; attributes [ii] != NULL; ii ++) {
107 msg += attributes [ii];
112 Logger::debug(msg, ANNA_FILE_LOCATION);
115 delete [] attributes;
119 string Search::asString() const
121 string result("ldap::Search { ");
122 result += Request::asString();
123 result += " | 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 += " }";
134 xml::Node* Search::asXML(xml::Node* parent) const
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);