PTLib
Version 2.10.10
Main Page
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Pages
pldap.h
Go to the documentation of this file.
1
/*
2
* pldap.h
3
*
4
* Lightweight Directory Access Protocol interface class.
5
*
6
* Portable Windows Library
7
*
8
* Copyright (c) 1993-2003 Equivalence Pty. Ltd.
9
*
10
* The contents of this file are subject to the Mozilla Public License
11
* Version 1.0 (the "License"); you may not use this file except in
12
* compliance with the License. You may obtain a copy of the License at
13
* http://www.mozilla.org/MPL/
14
*
15
* Software distributed under the License is distributed on an "AS IS"
16
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17
* the License for the specific language governing rights and limitations
18
* under the License.
19
*
20
* The Original Code is Portable Windows Library.
21
*
22
* The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23
*
24
* Contributor(s): ______________________________________.
25
*
26
* $Revision: 27535 $
27
* $Author: rjongbloed $
28
* $Date: 2012-04-26 02:48:22 -0500 (Thu, 26 Apr 2012) $
29
*/
30
31
#ifndef PTLIB_PLDAP_H
32
#define PTLIB_PLDAP_H
33
34
#ifdef P_USE_PRAGMA
35
#pragma interface
36
#endif
37
38
#if defined(P_LDAP) && !defined(_WIN32_WCE)
39
40
#include <
ptlib/sockets.h
>
41
#include <
ptlib/pluginmgr.h
>
42
#include <map>
43
#include <list>
44
45
struct
ldap;
46
struct
ldapmsg;
47
struct
ldapmod;
48
struct
berval;
49
50
class
PLDAPStructBase
;
51
52
55
class
PLDAPSession
:
public
PObject
56
{
57
PCLASSINFO(
PLDAPSession
,
PObject
);
58
public
:
61
PLDAPSession
(
62
const
PString
&
defaultBaseDN
=
PString::Empty
()
63
);
64
67
~PLDAPSession
();
68
75
PBoolean
Open
(
76
const
PString
& server,
77
WORD port = 0
78
);
79
82
PBoolean
Close
();
83
86
PBoolean
IsOpen
()
const
{
return
ldapContext
!= NULL; }
87
90
PBoolean
SetOption
(
91
int
optcode,
92
int
value
93
);
94
97
PBoolean
SetOption
(
98
int
optcode,
99
void
* value
100
);
101
102
enum
AuthenticationMethod
{
103
AuthSimple
,
104
AuthSASL
,
105
AuthKerberos
,
106
#ifdef SOLARIS
107
NumAuthenticationMethod1,
108
NumAuthenticationMethod2
109
#else
110
NumAuthenticationMethod
111
#endif
112
};
113
116
PBoolean
StartTLS
();
117
120
PBoolean
Bind
(
121
const
PString
& who =
PString::Empty
(),
122
const
PString
& passwd =
PString::Empty
(),
123
AuthenticationMethod
authMethod =
AuthSimple
124
);
125
126
class
ModAttrib
:
public
PObject
{
127
PCLASSINFO(
ModAttrib
,
PObject
);
128
public
:
129
enum
Operation
{
130
Add
,
131
Replace
,
132
Delete
,
133
NumOperations
134
};
135
136
protected
:
137
ModAttrib
(
138
const
PString
&
name
,
139
Operation
op
=
NumOperations
140
);
141
142
public
:
143
const
PString
&
GetName
()
const
{
return
name
; }
144
145
Operation
GetOperation
()
const
{
return
op
; }
146
147
void
SetLDAPMod
(
148
struct
ldapmod & mod,
149
Operation
defaultOp
150
);
151
152
protected
:
153
virtual
PBoolean
IsBinary
()
const
= 0;
154
virtual
void
SetLDAPModVars
(
struct
ldapmod & mod) = 0;
155
156
PString
name
;
157
Operation
op
;
158
};
159
160
class
StringModAttrib
:
public
ModAttrib
{
161
PCLASSINFO(
StringModAttrib
,
ModAttrib
);
162
public
:
163
StringModAttrib
(
164
const
PString
&
name
,
165
Operation
op
=
NumOperations
166
);
167
StringModAttrib
(
168
const
PString
& name,
169
const
PString
& value,
170
Operation
op
=
NumOperations
171
);
172
StringModAttrib
(
173
const
PString
& name,
174
const
PStringList
&
values
,
175
Operation
op
=
NumOperations
176
);
177
void
SetValue
(
178
const
PString
& value
179
);
180
void
AddValue
(
181
const
PString
& value
182
);
183
protected
:
184
virtual
PBoolean
IsBinary
()
const
;
185
virtual
void
SetLDAPModVars
(
struct
ldapmod & mod);
186
187
PStringArray
values
;
188
PBaseArray<char *>
pointers
;
189
};
190
191
class
BinaryModAttrib
:
public
ModAttrib
{
192
PCLASSINFO(
BinaryModAttrib
,
ModAttrib
);
193
public
:
194
BinaryModAttrib
(
195
const
PString
&
name
,
196
Operation
op
=
Add
197
);
198
BinaryModAttrib
(
199
const
PString
& name,
200
const
PBYTEArray
& value,
201
Operation
op
=
Add
202
);
203
BinaryModAttrib
(
204
const
PString
& name,
205
const
PArray<PBYTEArray>
&
values
,
206
Operation
op
=
Add
207
);
208
void
SetValue
(
209
const
PBYTEArray
& value
210
);
211
void
AddValue
(
212
const
PBYTEArray
& value
213
);
214
protected
:
215
virtual
PBoolean
IsBinary
()
const
;
216
virtual
void
SetLDAPModVars
(
struct
ldapmod & mod);
217
218
PArray<PBYTEArray>
values
;
219
PBaseArray<struct berval *>
pointers
;
220
PBYTEArray
bervals
;
221
};
222
225
PBoolean
Add
(
226
const
PString
& dn,
227
const
PArray<ModAttrib>
& attributes
228
);
229
232
PBoolean
Add
(
233
const
PString
& dn,
234
const
PStringToString
& attributes
235
);
236
240
PBoolean
Add
(
241
const
PString
& dn,
242
const
PStringArray
& attributes
243
);
244
248
PBoolean
Add
(
249
const
PString
& dn,
250
const
PLDAPStructBase
& data
251
);
252
255
PBoolean
Modify
(
256
const
PString
& dn,
257
const
PArray<ModAttrib>
& attributes
258
);
259
262
PBoolean
Modify
(
263
const
PString
& dn,
264
const
PStringToString
& attributes
265
);
266
270
PBoolean
Modify
(
271
const
PString
& dn,
272
const
PStringArray
& attributes
273
);
274
278
PBoolean
Modify
(
279
const
PString
& dn,
280
const
PLDAPStructBase
& data
281
);
282
285
PBoolean
Delete
(
286
const
PString
& dn
287
);
288
289
290
enum
SearchScope
{
291
ScopeBaseOnly
,
292
ScopeSingleLevel
,
293
ScopeSubTree
,
294
NumSearchScope
295
};
296
297
class
SearchContext
{
298
public
:
299
SearchContext
();
300
~SearchContext
();
301
302
PBoolean
IsCompleted
()
const
{
return
completed; }
303
304
private
:
305
int
msgid;
306
struct
ldapmsg * result;
307
struct
ldapmsg * message;
308
PBoolean
found;
309
PBoolean
completed;
310
311
friend
class
PLDAPSession
;
312
};
313
316
PBoolean
Search
(
317
SearchContext
& context,
318
const
PString
& filter,
319
const
PStringArray
& attributes =
PStringList
(),
320
const
PString
& base =
PString::Empty
(),
321
SearchScope
scope =
ScopeSubTree
322
);
323
326
PBoolean
GetSearchResult
(
327
SearchContext
& context,
328
PStringToString
& data
329
);
330
333
PBoolean
GetSearchResult
(
334
SearchContext
& context,
335
const
PString
& attribute,
336
PString
& data
337
);
338
341
PBoolean
GetSearchResult
(
342
SearchContext
& context,
343
const
PString
& attribute,
344
PStringArray
& data
345
);
346
349
PBoolean
GetSearchResult
(
350
SearchContext
& context,
351
const
PString
& attribute,
352
PArray<PBYTEArray>
& data
353
);
354
357
PBoolean
GetSearchResult
(
358
SearchContext
& context,
359
PLDAPStructBase
& data
360
);
361
364
PString
GetSearchResultDN
(
365
SearchContext
& context
366
);
367
370
PBoolean
GetNextSearchResult
(
371
SearchContext
& context
372
);
373
378
PList<PStringToString>
Search
(
379
const
PString
& filter,
380
const
PStringArray
& attributes =
PStringList
(),
381
const
PString
& base =
PString::Empty
(),
382
SearchScope
scope =
ScopeSubTree
383
);
384
385
388
void
SetBaseDN
(
389
const
PString
& dn
390
) {
defaultBaseDN
= dn; }
391
394
const
PString
&
GetBaseDN
()
const
{
return
defaultBaseDN
; }
395
398
int
GetErrorNumber
()
const
{
return
errorNumber
; }
399
402
PString
GetErrorText
()
const
;
403
406
struct
ldap *
GetOpenLDAP
()
const
{
return
ldapContext
; }
407
410
const
PTimeInterval
&
GetTimeout
()
const
{
return
timeout
; }
411
414
void
SetTimeout
(
415
const
PTimeInterval
& t
416
) {
timeout
= t; }
417
420
void
SetSearchLimit
(
421
const
unsigned
s
422
) {
searchLimit
= s; }
423
424
protected
:
425
struct
ldap *
ldapContext
;
426
int
errorNumber
;
427
unsigned
protocolVersion
;
428
PString
defaultBaseDN
;
429
unsigned
searchLimit
;
430
PTimeInterval
timeout
;
431
PString
multipleValueSeparator
;
432
};
433
434
435
436
class
PLDAPStructBase
;
437
438
class
PLDAPAttributeBase
:
public
PObject
439
{
440
PCLASSINFO(
PLDAPAttributeBase
,
PObject
);
441
public
:
442
PLDAPAttributeBase
(
const
char
*
name
,
void
*
pointer
, PINDEX
size
);
443
444
const
char
*
GetName
()
const
{
return
name
; }
445
PBoolean
IsBinary
()
const
{
return
pointer != NULL; }
446
447
virtual
void
Copy
(
const
PLDAPAttributeBase
& other) = 0;
448
449
virtual
PString
ToString
()
const
;
450
virtual
void
FromString
(
const
PString
& str);
451
virtual
PBYTEArray
ToBinary
()
const
;
452
virtual
void
FromBinary
(
const
PArray<PBYTEArray>
& data);
453
454
protected
:
455
const
char
*
name
;
456
void
*
pointer
;
457
PINDEX
size
;
458
};
459
460
461
class
PLDAPStructBase
:
public
PObject
{
462
PCLASSINFO(
PLDAPStructBase
,
PObject
);
463
protected
:
464
PLDAPStructBase
();
465
PLDAPStructBase
&
operator=
(
const
PLDAPStructBase
&);
466
PLDAPStructBase
&
operator=
(
const
PStringArray
& array);
467
PLDAPStructBase
&
operator=
(
const
PStringToString
& dict);
468
private
:
469
PLDAPStructBase
(
const
PLDAPStructBase
& obj) :
PObject
(obj) { }
470
471
public
:
472
void
PrintOn
(ostream & strm)
const
;
473
474
PINDEX
GetNumAttributes
()
const
{
return
attributes
.
GetSize
(); }
475
PLDAPAttributeBase
&
GetAttribute
(PINDEX idx)
const
{
return
attributes
.
GetDataAt
(idx); }
476
PLDAPAttributeBase
*
GetAttribute
(
const
char
* name)
const
{
return
attributes
.
GetAt
(name); }
477
478
void
AddAttribute
(
PLDAPAttributeBase
* var);
479
static
PLDAPStructBase
&
GetInitialiser
() {
return
*
PAssertNULL
(
initialiserInstance
); }
480
481
protected
:
482
void
EndConstructor
();
483
484
PDictionary<PString, PLDAPAttributeBase>
attributes
;
485
486
PLDAPStructBase
*
initialiserStack
;
487
static
PMutex
initialiserMutex
;
488
static
PLDAPStructBase
*
initialiserInstance
;
489
};
490
492
493
class
PLDAPSchema
:
public
PObject
494
{
495
public
:
496
PLDAPSchema
();
497
498
enum
AttributeType
{
499
AttibuteUnknown
= -1,
500
AttributeString
,
501
AttributeBinary
,
502
AttributeNumeric
503
};
504
505
class
Attribute
506
{
507
public
:
508
Attribute
() :
m_type
(
AttibuteUnknown
) { }
509
Attribute
(
const
PString
& name,
AttributeType
type);
510
PString
m_name
;
511
AttributeType
m_type
;
512
};
513
514
typedef
std::list<Attribute>
attributeList
;
515
516
static
PLDAPSchema
*
CreateSchema
(
const
PString
& schemaname,
PPluginManager
* pluginMgr = NULL);
517
static
PStringList
GetSchemaNames
(
PPluginManager
* pluginMgr = NULL);
518
static
PStringList
GetSchemaFriendlyNames
(
const
PString
& schema,
PPluginManager
* pluginMgr = NULL);
519
520
void
OnReceivedAttribute
(
const
PString
& attribute,
const
PString
& value);
521
522
void
OnSendSchema
(
PArray<PLDAPSession::ModAttrib>
&
attributes
,
523
PLDAPSession::ModAttrib::Operation
op=
PLDAPSession::ModAttrib::Add
);
524
525
void
LoadSchema
();
526
527
PStringList
SchemaName
() {
return
PStringList
(); }
528
virtual
void
AttributeList
(
attributeList
&
/*attrib*/
) {};
529
530
531
PStringList
GetAttributeList
();
532
PBoolean
Exists
(
const
PString
& attribute);
533
534
PBoolean
SetAttribute
(
const
PString
& attribute,
const
PString
& value);
535
PBoolean
SetAttribute
(
const
PString
& attribute,
const
PBYTEArray
& value);
536
537
PBoolean
GetAttribute
(
const
PString
& attribute,
PString
& value);
538
PBoolean
GetAttribute
(
const
PString
& attribute,
PBYTEArray
& value);
539
540
AttributeType
GetAttributeType
(
const
PString
& attribute);
541
542
543
protected
:
544
typedef
std::map<PString,PString>
ldapAttributes
;
545
typedef
std::map<PString,PBYTEArray>
ldapBinAttributes
;
546
547
548
attributeList
attributelist
;
549
ldapAttributes
attributes
;
550
ldapBinAttributes
binattributes
;
551
};
552
553
554
template
<
class
class
Name>
class
LDAPPluginServiceDescriptor
:
public
PDevicePluginServiceDescriptor
555
{
556
public
:
557
virtual
PObject
*
CreateInstance
(
int
/*userData*/
)
const
{
return
new
className; }
558
virtual
PStringArray
GetDeviceNames
(
int
/*userData*/
)
const
{
return
className::SchemaName(); }
559
};
560
561
#define LDAP_Schema(name) \
562
static LDAPPluginServiceDescriptor<name##_schema> name##_schema_descriptor; \
563
PCREATE_PLUGIN(name##_schema, PLDAPSchema, &name##_schema_descriptor)
564
566
567
#define PLDAP_STRUCT_BEGIN(name) \
568
class name : public PLDAPStructBase { \
569
public: name() : PLDAPStructBase() { EndConstructor(); } \
570
public: name(const name & other) : PLDAPStructBase() { EndConstructor(); operator=(other); } \
571
public: name(const PStringArray & array) : PLDAPStructBase() { EndConstructor(); operator=(array); } \
572
public: name(const PStringToString & dict) : PLDAPStructBase() { EndConstructor(); operator=(dict); } \
573
public: name & operator=(const name & other) { PLDAPStructBase::operator=(other); return *this; } \
574
public: name & operator=(const PStringArray & array) { PLDAPStructBase::operator=(array); return *this; } \
575
public: name & operator=(const PStringToString & dict) { PLDAPStructBase::operator=(dict); return *this; } \
576
PLDAP_ATTR_INIT(name, PString, objectClass, #name);
577
578
#define PLDAP_ATTRIBUTE(base, type, attribute, pointer, init) \
579
public: type attribute; \
580
private: struct PLDAPAttr_##attribute : public PLDAPAttributeBase { \
581
PLDAPAttr_##attribute() \
582
: PLDAPAttributeBase(#attribute, pointer, sizeof(type)), \
583
instance(((base &)base::GetInitialiser()).attribute) \
584
{ init } \
585
virtual void PrintOn (ostream & s) const { s << instance; } \
586
virtual void ReadFrom(istream & s) { s >> instance; } \
587
virtual void Copy(const PLDAPAttributeBase & other) \
588
{ instance = ((PLDAPAttr_##attribute &)other).instance; } \
589
type & instance; \
590
} pldapvar_##attribute
591
592
#define PLDAP_ATTR_SIMP(base, type, attribute) \
593
PLDAP_ATTRIBUTE(base, type, attribute, NULL, ;)
594
595
#define PLDAP_ATTR_INIT(base, type, attribute, init) \
596
PLDAP_ATTRIBUTE(base, type, attribute, NULL, instance = init;)
597
598
#define PLDAP_BINATTRIB(base, type, attribute) \
599
PLDAP_ATTRIBUTE(base, type, attribute, &((base &)base::GetInitialiser()).attribute, ;)
600
601
#define PLDAP_STRUCT_END() \
602
};
603
604
#endif // P_LDAP
605
606
#endif // PTLIB_PLDAP_H
607
608
609
// End of file ////////////////////////////////////////////////////////////////
include
ptclib
pldap.h
Generated on Mon Mar 4 2013 13:14:09 for PTLib by
1.8.3.1