You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
4.8 KiB

/*
$Id: ldap1.cpp,v 1.1.1.1 2005/07/07 15:05:59 oflebbe Exp $
Copyright (C) 2003 Olaf Flebbe, Science and Computing AG
o.flebbe@science-computing.de
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define WINDOWS_MEAN_AND_LEAN
#define UNICODE
#include "ldap1.h"
void
CLDAP::do_query( const mystring& filter, const wchar_t *attrs[]) {
if (fp) {
fprintf(fp, "do_query %S\n", filter.c_str());
fflush(fp);
}
// this is extremly ugly: Why can't the compile not cast a wchar_t * to a PWCHAR???
ldap_search_s( lp, (const PWCHAR) context.c_str(), LDAP_SCOPE_SUBTREE,
(const PWCHAR) filter.c_str(), (PWCHAR *) attrs, 0, &msg);
if (fp) {
fprintf(fp, "after ldap_search\n");
fflush(fp);
}
if (msg != NULL) {
msg = ldap_first_entry( lp, msg);
}
}
mystring
CLDAP::queryAttr( const mystring &filter, const mystring &attr, bool *exists) {
const wchar_t *attrs[]= { attr.c_str(), NULL} ; //= { attr.c_str(), NULL };
do_query( filter, attrs);
if (exists != NULL)
*exists = true;
while ( msg != NULL) {
BerElement *berPtr;
wchar_t *attrPtr = ldap_first_attribute( lp, msg, &berPtr);
while (attrPtr != NULL) {
wchar_t **valList = ldap_get_values( lp, msg, attrPtr);
for (unsigned int i = 0; i < ldap_count_values( valList); i++)
return mystring( valList[i]);
attrPtr = ldap_next_attribute( lp, msg, berPtr);
}
}
if (exists != NULL)
*exists = false;
return mystring(L"");
}
stringSet
CLDAP::queryListOfAttr( const mystring &filter, const mystring &attr) {
stringSet listOfVal;
const wchar_t *attrs[]= { attr.c_str(), NULL} ; //= { attr.c_str(), NULL };
do_query( filter, attrs);
while ( msg != NULL) {
BerElement *berPtr;
wchar_t *attrPtr = ldap_first_attribute( lp, msg, &berPtr);
while (attrPtr != NULL) {
wchar_t **valList = ldap_get_values( lp, msg, attrPtr);
for (unsigned int i = 0; i < ldap_count_values( valList); i++)
listOfVal.insert( mystring( valList[i]));
attrPtr = ldap_next_attribute( lp, msg, berPtr);
}
msg = ldap_next_entry( lp, msg);
}
return listOfVal;
}
stringMap
CLDAP::querySetOfAttrs( const mystring &filter, const stringSet &attr) {
stringMap mapOfVal;
const wchar_t **attrs;
attrs = (const wchar_t **) malloc( sizeof( wchar_t *) * (attr.size()+1));
for (unsigned int i = 0; i < attr.size()+1; i++)
attrs[i] = NULL;
const wchar_t **pat = attrs;
for (stringSet::const_iterator ptr = attr.begin(); ptr != attr.end(); ptr++) {
*pat++ = ptr->c_str();
}
do_query( filter, attrs);
while ( msg != NULL) {
BerElement *berPtr;
wchar_t *attrPtr = ldap_first_attribute( lp, msg, &berPtr);
while (attrPtr != NULL) {
wchar_t **valList = ldap_get_values( lp, msg, attrPtr);
mapOfVal[ mystring( attrPtr)] = mystring( valList[0]);
attrPtr = ldap_next_attribute( lp, msg, berPtr);
}
msg = ldap_next_entry( lp, msg);
}
return mapOfVal;
}
CLDAP::CLDAP( const std::list<mystring>& servers, FILE *fp, const mystring& binddn, const mystring& bindpasswd) {
this->fp = fp;
msg = NULL;
for ( std::list<mystring>::const_iterator ptr = servers.begin(); ptr != servers.end(); ptr++) {
lp = ldap_init( (const PWCHAR) ptr->c_str(), LDAP_PORT);
ULONG version = LDAP_VERSION3;
if (!lp) {
if (fp) {
fprintf( fp, "ldap_init error on server %S\n", ptr->c_str());
}
continue;
}
int ret = ldap_set_option( lp, LDAP_OPT_VERSION, &version);
if (ret != LDAP_SUCCESS) {
if (fp) {
fprintf( fp, "ldap_set_option error %x on server %S\n", ret, ptr->c_str());
}
ldap_unbind( lp);
continue;
}
if (binddn == L"" || bindpasswd == L"") {
ret = ldap_simple_bind_s( lp, NULL, NULL);
if (LDAP_SUCCESS != ret) {
if (fp) {
fprintf( fp, "anonymous ldap_simple_bind_s error %x on server %S\n", ret, ptr->c_str());
}
ldap_unbind( lp);
lp = NULL;
}
} else {
ret = ldap_simple_bind_s( lp, (PWCHAR) binddn.c_str(), (PWCHAR) bindpasswd.c_str());
if (LDAP_SUCCESS != ret) {
if (fp) {
fprintf( fp, "ldap_simple_bind_s error %x on server %S, basedn %S, passwd %S\n",
ret, ptr->c_str(), binddn.c_str(), bindpasswd.c_str());
}
ldap_unbind( lp);
lp = NULL;
}
}
return;
}
}