1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
/*
$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;
}
}
|