16 #include <boost/tokenizer.hpp>
23 LOG(
DEBUG) <<
"WsLdapAuth::WsLdapAuthInit() : Preparing required libraries : libgdcore ";
24 void* hndl = dlopen(
"libgdcore.so", RTLD_NOW | RTLD_GLOBAL);
26 LOG(
ERROR) <<
"WsLdapAuth::WsLdapAuthInit() : Cannot load libgdcore.so shared library! " << dlerror();
33 m_ldapStatus = notLoaded;
43 if (!ipValid(ip) || uid == guest) {
44 LOG(
INFO) <<
"WsLdapAuth::authentify() : IP not valid " << ip <<
" For user " << uid;
46 int ran = rand() % 10000000 + 1;
47 m_uid = m_uid +
"-" + boost::lexical_cast<
string>(ran);
48 m_groups.insert(guest);
52 m_ldapStatus = notLoaded;
54 gdCLdapServer cLdapServer;
55 cLdapServer.strServer = m_properties->get(
"ldap",
"server",
"");
56 cLdapServer.strDisplayName = m_properties->get(
"ldap",
"server_name",
"");
59 gdCLdapAttributes cWitchAttrs;
60 gdCLdapFilter cFilter;
61 gdCLdapEntries cEntries;
62 cFilter.pServer = &cLdapServer;
63 cFilter.nScope = LDAP_SCOPE_SUB;
65 cFilter.strInitialSearchBase = m_properties->get(
"ldap",
"search_base_uid",
"");
66 LOG(
DEBUG) <<
"WsLdapAuth :: getting filter for uid " << m_uid;
67 cFilter.strFilter = m_properties->getAndReplace(
"ldap",
"filter_uid", m_uid,
"");
68 LOG(
DEBUG) <<
"WsLdapAuth :: Filter for uid " << cFilter.strFilter;
69 cWitchAttrs.push_back(
new gdCLdapAttribute(m_properties->get(
"ldap",
"sn_key",
"")));
70 cWitchAttrs.push_back(
new gdCLdapAttribute(m_properties->get(
"ldap",
"gn_key",
"")));
71 cWitchAttrs.push_back(
new gdCLdapAttribute(m_properties->get(
"ldap",
"mail_key",
"")));
72 cWitchAttrs.push_back(
new gdCLdapAttribute(
"dn"));
73 if ( !cLdap.GetInfo(cFilter, cWitchAttrs, cEntries) ) {
74 LOG(
ERROR) <<
"WsLdapAuth::authentify() : Could not query ldap !" << endl;
75 m_ldapStatus = onError;
77 for (
int i = 0; i < cWitchAttrs.size(); ++i) {
78 delete cWitchAttrs[i];
82 if ( cEntries.size() != 1 ) {
83 LOG(
ERROR) <<
"WsLdapAuth::authentify() : Null or too much entries when querying LDAP !" << endl;
84 m_ldapStatus = onError;
85 for (
int i = 0; i < cWitchAttrs.size(); ++i) {
86 delete cWitchAttrs[i];
91 for (
int nEntry = 0; nEntry < cEntries.size(); nEntry++) {
92 for (
int nAttr = 0; nAttr < cEntries[nEntry]->Attrs.size(); nAttr++) {
93 if ( cEntries[nEntry]->Attrs[nAttr]->Attr == m_properties->get(
"ldap",
"sn_key",
"")) {
94 m_surname = cEntries[nEntry]->Attrs[nAttr]->Values[0]->ToStr();
97 if ( cEntries[nEntry]->Attrs[nAttr]->Attr == m_properties->get(
"ldap",
"gn_key",
"")) {
98 m_name = cEntries[nEntry]->Attrs[nAttr]->Values[0]->ToStr();
101 if ( cEntries[nEntry]->Attrs[nAttr]->Attr == m_properties->get(
"ldap",
"mail_key",
"")) {
102 m_email = cEntries[nEntry]->Attrs[nAttr]->Values[0]->ToStr();
105 if ( cEntries[nEntry]->Attrs[nAttr]->Attr ==
"dn" ) {
106 m_dn = cEntries[nEntry]->Attrs[nAttr]->Values[0]->ToStr();
112 if ( isPublicSite && uid.size() > 0 ) {
113 cLdapServer.DisConnect();
114 cLdapServer.strLogon = m_dn;
115 cLdapServer.strPassword = pass;
116 if ( !cLdapServer.Connect() ) {
117 LOG(
DEBUG) <<
"WsLdapAuth::authentify() : Cannot connect with uid " << m_uid;
118 m_ldapStatus = onError;
121 LOG(
DEBUG) <<
"WsLdapAuth :: Connected with dn " << m_dn;
124 cFilter.strInitialSearchBase = m_properties->get(
"ldap",
"search_base_groups",
"");
125 cFilter.strFilter = m_properties->getAndReplace(
"ldap",
"filter_groups", m_uid,
"");
126 LOG(
DEBUG) <<
"WsLdapAuth::authentify() : Filter for groups " << cFilter.strFilter;
127 gdCLdapAttributes cWitchAttrs2;
128 gdCLdapEntries cEntries2;
129 cWitchAttrs2.push_back(
new gdCLdapAttribute(m_properties->get(
"ldap",
"groups_key",
"")));
130 cLdap.setManageDSAIT(
true);
131 if ( !cLdap.GetInfo(cFilter, cWitchAttrs2, cEntries2) ) {
132 LOG(
ERROR) <<
"WsLdapAuth::authentify() : Could not query ldap !";
133 m_ldapStatus = onError;
134 for (
int i = 0 ; i < cWitchAttrs2.size(); ++i) {
135 delete cWitchAttrs[i];
139 LOG(
DEBUG) <<
"WsLdapAuth::authentify() : groups entries size : " << cEntries2.size();
140 for (
int nEntry = 0; nEntry < cEntries2.size(); nEntry++) {
141 for (
int nAttr = 0; nAttr < cEntries2[nEntry]->Attrs.size(); nAttr++) {
142 if ( cEntries2[nEntry]->Attrs[nAttr]->Attr == m_properties->get(
"ldap",
"groups_key",
"")) {
143 for (
int nVal = 0; nVal < cEntries2[nEntry]->Attrs[nAttr]->Values.size(); nVal++) {
144 m_groups.insert(cEntries2[nEntry]->Attrs[nAttr]->Values[nVal]->ToStr().c_str());
150 for (
int i = 0 ; i < cWitchAttrs2.size(); ++i) {
151 delete cWitchAttrs[i];
153 m_ldapStatus = loaded;
184 m_ldapStatus = notLoaded;
186 gdCLdapServer cLdapServer;
187 cLdapServer.strServer = m_properties->get(
"ldap",
"server",
"");
188 cLdapServer.strDisplayName = m_properties->get(
"ldap",
"server_name",
"");
191 gdCLdapFilter cFilter;
192 cFilter.pServer = &cLdapServer;
193 cFilter.nScope = LDAP_SCOPE_SUB;
195 cFilter.strInitialSearchBase = m_properties->get(
"ldap",
"search_base_allgroups",
"");
196 cFilter.strFilter = m_properties->get(
"ldap",
"filter_allgroups",
"");
197 gdCLdapAttributes cWitchAttrs2;
198 gdCLdapEntries cEntries2;
199 cWitchAttrs2.push_back(
new gdCLdapAttribute(m_properties->get(
"ldap",
"allgroups_key",
"")));
200 cLdap.setManageDSAIT(
true);
201 if ( !cLdap.GetInfo(cFilter, cWitchAttrs2, cEntries2) ) {
202 LOG(
ERROR) <<
"WsLdapAuth::authentify() : Could not query ldap !";
203 m_ldapStatus = onError;
204 return set<string>();
206 for (
int nEntry = 0; nEntry < cEntries2.size(); nEntry++) {
207 for (
int nAttr = 0; nAttr < cEntries2[nEntry]->Attrs.size(); nAttr++) {
208 if ( cEntries2[nEntry]->Attrs[nAttr]->Attr == m_properties->get(
"ldap",
"allgroups_key",
"")) {
209 for (
int nVal = 0; nVal < cEntries2[nEntry]->Attrs[nAttr]->Values.size(); nVal++) {
210 m_allGroups.insert(cEntries2[nEntry]->Attrs[nAttr]->Values[nVal]->ToStr().c_str());
216 m_ldapStatus = loaded;
225 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
226 boost::char_separator<char> sep(
".");
227 tokenizer maskTok(ipMask, sep);
228 tokenizer uipTok(ip, sep);
229 tokenizer::iterator mask_iter = maskTok.begin();
230 tokenizer::iterator ip_iter = uipTok.begin();
231 for (; ip_iter != uipTok.end(); ++mask_iter, ++ip_iter) {
232 if (mask_iter == maskTok.end())
234 if (*mask_iter ==
"*")
236 if (*ip_iter != *mask_iter)
bool ipValid(std::string ip)
return true or false whether the ip is valid (ie match the mask in the configuration file ...
int authentify(const std::string &uid, const std::string &pass="", const std::string &ip="")
authenticate the user by retrieving his info from ldap
std::set< std::string > getUserGroups()
get the groups of the user
std::string getSurname()
Get the surname of the user.
std::string get(const std::string §ion, const std::string &id, const std::string &def)
static WsGlobalProperties * instance()
std::string getUid()
Get the uid of the user.
std::string getFirstName()
Get the first name of the user.
std::set< std::string > getAllGroups()
get all the groups in the ldap
void WsLdapAuthInit(void)
std::string getEmail()
Get the email of the user.