PTLib  Version 2.18.8
 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 
27 #ifndef PTLIB_PLDAP_H
28 #define PTLIB_PLDAP_H
29 
30 #ifdef P_USE_PRAGMA
31 #pragma interface
32 #endif
33 
34 #if defined(P_LDAP)
35 
36 #include <ptlib/sockets.h>
37 #include <ptlib/pluginmgr.h>
38 #include <map>
39 #include <list>
40 
41 struct ldap;
42 struct ldapmsg;
43 struct ldapmod;
44 struct berval;
45 
46 class PLDAPStructBase;
47 
48 
51 class PLDAPSession : public PObject
52 {
53  PCLASSINFO(PLDAPSession, PObject);
54  public:
58  const PString & defaultBaseDN = PString::Empty()
59  );
60 
63  ~PLDAPSession();
64 
71  PBoolean Open(
72  const PString & server,
73  WORD port = 0
74  );
75 
78  PBoolean Close();
79 
82  PBoolean IsOpen() const { return m_ldapContext != NULL; }
83 
87  int optcode,
88  int value
89  );
90 
94  int optcode,
95  void * value
96  );
97 
102 #ifdef SOLARIS
103  NumAuthenticationMethod1,
104  NumAuthenticationMethod2
105 #else
107 #endif
108  };
109 
112  PBoolean StartTLS();
113 
116  PBoolean Bind(
117  const PString & who = PString::Empty(),
118  const PString & passwd = PString::Empty(),
119  AuthenticationMethod authMethod = AuthSimple
120  );
121 
122  class ModAttrib : public PObject {
123  PCLASSINFO(ModAttrib, PObject);
124  public:
125  enum Operation {
130  };
131 
132  protected:
133  ModAttrib(
134  const PString & name,
136  );
137 
138  public:
139  const PString & GetName() const { return m_name; }
140 
141  Operation GetOperation() const { return m_op; }
142 
143  void SetLDAPMod(
144  struct ldapmod & mod,
145  Operation defaultOp
146  );
147 
148  protected:
149  virtual PBoolean IsBinary() const = 0;
150  virtual void SetLDAPModVars(struct ldapmod & mod) = 0;
151 
154  };
155 
156  class StringModAttrib : public ModAttrib {
157  PCLASSINFO(StringModAttrib, ModAttrib);
158  public:
160  const PString & name,
162  );
164  const PString & name,
165  const PString & value,
167  );
169  const PString & name,
170  const PStringList & values,
172  );
173  void SetValue(
174  const PString & value
175  );
176  void AddValue(
177  const PString & value
178  );
179  protected:
180  virtual PBoolean IsBinary() const;
181  virtual void SetLDAPModVars(struct ldapmod & mod);
182 
185  };
186 
187  class BinaryModAttrib : public ModAttrib {
188  PCLASSINFO(BinaryModAttrib, ModAttrib);
189  public:
191  const PString & name,
192  Operation op = Add
193  );
195  const PString & name,
196  const PBYTEArray & value,
197  Operation op = Add
198  );
200  const PString & name,
201  const PArray<PBYTEArray> & values,
202  Operation op = Add
203  );
204  void SetValue(
205  const PBYTEArray & value
206  );
207  void AddValue(
208  const PBYTEArray & value
209  );
210  protected:
211  virtual PBoolean IsBinary() const;
212  virtual void SetLDAPModVars(struct ldapmod & mod);
213 
217  };
218 
221  PBoolean Add(
222  const PString & dn,
223  const PArray<ModAttrib> & attributes
224  );
225 
228  PBoolean Add(
229  const PString & dn,
230  const PStringToString & attributes
231  );
232 
236  PBoolean Add(
237  const PString & dn,
238  const PStringArray & attributes
239  );
240 
244  PBoolean Add(
245  const PString & dn,
246  const PLDAPStructBase & data
247  );
248 
252  const PString & dn,
253  const PArray<ModAttrib> & attributes
254  );
255 
259  const PString & dn,
260  const PStringToString & attributes
261  );
262 
267  const PString & dn,
268  const PStringArray & attributes
269  );
270 
275  const PString & dn,
276  const PLDAPStructBase & data
277  );
278 
282  const PString & dn
283  );
284 
285 
286  enum SearchScope {
291  };
292 
294  public:
295  SearchContext();
296  ~SearchContext();
297 
298  PBoolean IsCompleted() const { return m_completed; }
299 
300  private:
301  int m_msgid;
302  struct ldapmsg * m_result;
303  struct ldapmsg * m_message;
304  bool m_found;
305  bool m_completed;
306 
307  friend class PLDAPSession;
308  };
309 
313  SearchContext & context,
314  const PString & filter,
315  const PStringArray & attributes = PStringList(),
316  const PString & base = PString::Empty(),
317  SearchScope scope = ScopeSubTree
318  );
319 
323  SearchContext & context,
324  PStringToString & data
325  );
326 
330  SearchContext & context,
331  const PString & attribute,
332  PString & data
333  );
334 
338  SearchContext & context,
339  const PString & attribute,
340  PStringArray & data
341  );
342 
346  SearchContext & context,
347  const PString & attribute,
348  PArray<PBYTEArray> & data
349  );
350 
354  SearchContext & context,
355  PLDAPStructBase & data
356  );
357 
361  SearchContext & context
362  );
363 
367  SearchContext & context
368  );
369 
375  const PString & filter,
376  const PStringArray & attributes = PStringList(),
377  const PString & base = PString::Empty(),
378  SearchScope scope = ScopeSubTree
379  );
380 
381 
384  void SetBaseDN(
385  const PString & dn
386  ) { m_defaultBaseDN = dn; }
387 
390  const PString & GetBaseDN() const { return m_defaultBaseDN; }
391 
394  int GetErrorNumber() const { return m_errorNumber; }
395 
398  PString GetErrorText() const;
399 
402  struct ldap * GetOpenLDAP() const { return m_ldapContext; }
403 
406  const PTimeInterval & GetTimeout() const { return m_timeout; }
407 
411  const PTimeInterval & t
412  ) { m_timeout = t; }
413 
417  const unsigned s
418  ) { m_searchLimit = s; }
419 
420  protected:
421  struct ldap * m_ldapContext;
425  unsigned m_searchLimit;
428 };
429 
430 
431 
432 class PLDAPStructBase;
433 
435 {
436  PCLASSINFO(PLDAPAttributeBase, PObject);
437  public:
438  PLDAPAttributeBase(const char * name, void * pointer, PINDEX size);
439 
440  const char * GetName() const { return m_name; }
441  PBoolean IsBinary() const { return m_pointer != NULL; }
442 
443  virtual void Copy(const PLDAPAttributeBase & other) = 0;
444 
445  virtual PString ToString() const;
446  virtual void FromString(const PString & str);
447  virtual PBYTEArray ToBinary() const;
448  virtual void FromBinary(const PArray<PBYTEArray> & data);
449 
450  protected:
451  const char * m_name;
452  void * m_pointer;
453  PINDEX m_size;
454 };
455 
456 
457 class PLDAPStructBase : public PObject {
458  PCLASSINFO(PLDAPStructBase, PObject);
459  protected:
460  PLDAPStructBase();
462  PLDAPStructBase & operator=(const PStringArray & array);
463  PLDAPStructBase & operator=(const PStringToString & dict);
464  private:
465  PLDAPStructBase(const PLDAPStructBase & obj) : PObject(obj) { }
466 
467  public:
468  void PrintOn(ostream & strm) const;
469 
472  const AttribDict & GetAttributes() const { return m_attributes; }
473  PLDAPAttributeBase * GetAttribute(const char * name) const { return m_attributes.GetAt(name); }
474 
475  void AddAttribute(PLDAPAttributeBase * var);
477 
478  protected:
479  void EndConstructor();
480 
482 
486 };
487 
489 
490 class PLDAPSchema : public PObject
491 {
492  public:
493  PLDAPSchema();
494 
500  };
501 
502  class Attribute
503  {
504  public:
506  Attribute(const PString & name, AttributeType type);
509  };
510 
511  typedef std::list<Attribute> attributeList;
512 
513  static PLDAPSchema * CreateSchema(const PString & schemaname, PPluginManager * pluginMgr = NULL);
514  static PStringArray GetSchemaNames(PPluginManager * pluginMgr = NULL);
515  static PStringArray GetSchemaFriendlyNames(PPluginManager * pluginMgr = NULL);
516 
517  void OnReceivedAttribute(const PString & attribute, const PString & value);
518 
521 
522  void LoadSchema();
523 
525  virtual void AttributeList(attributeList & /*attrib*/) {};
526 
527 
529  PBoolean Exists(const PString & attribute);
530 
531  PBoolean SetAttribute(const PString & attribute, const PString & value);
532  PBoolean SetAttribute(const PString & attribute, const PBYTEArray & value);
533 
534  PBoolean GetAttribute(const PString & attribute, PString & value);
535  PBoolean GetAttribute(const PString & attribute, PBYTEArray & value);
536 
537  AttributeType GetAttributeType(const PString & attribute);
538 
539 
540  protected:
541  typedef std::map<PString,PString> ldapAttributes;
542  typedef std::map<PString,PBYTEArray> ldapBinAttributes;
543 
544 
548 };
549 
550 
552 
553 #define LDAP_Schema(name) PCREATE_PLUGIN(name, PLDAPSchema)
554 
555 
557 
558 #define PLDAP_STRUCT_BEGIN(name) \
559  class name : public PLDAPStructBase { \
560  public: name() : PLDAPStructBase() { EndConstructor(); } \
561  public: name(const name & other) : PLDAPStructBase() { EndConstructor(); operator=(other); } \
562  public: name(const PStringArray & array) : PLDAPStructBase() { EndConstructor(); operator=(array); } \
563  public: name(const PStringToString & dict) : PLDAPStructBase() { EndConstructor(); operator=(dict); } \
564  public: name & operator=(const name & other) { PLDAPStructBase::operator=(other); return *this; } \
565  public: name & operator=(const PStringArray & array) { PLDAPStructBase::operator=(array); return *this; } \
566  public: name & operator=(const PStringToString & dict) { PLDAPStructBase::operator=(dict); return *this; } \
567  PLDAP_ATTR_INIT(name, PString, objectClass, #name);
568 
569 #define PLDAP_ATTRIBUTE(base, type, attribute, pointer, init) \
570  public: type attribute; \
571  private: struct PLDAPAttr_##attribute : public PLDAPAttributeBase { \
572  PLDAPAttr_##attribute() \
573  : PLDAPAttributeBase(#attribute, pointer, sizeof(type)), \
574  instance(((base &)base::GetInitialiser()).attribute) \
575  { init } \
576  virtual void PrintOn (ostream & s) const { s << instance; } \
577  virtual void ReadFrom(istream & s) { s >> instance; } \
578  virtual void Copy(const PLDAPAttributeBase & other) \
579  { instance = ((PLDAPAttr_##attribute &)other).instance; } \
580  type & instance; \
581  } pldapvar_##attribute
582 
583 #define PLDAP_ATTR_SIMP(base, type, attribute) \
584  PLDAP_ATTRIBUTE(base, type, attribute, NULL, ;)
585 
586 #define PLDAP_ATTR_INIT(base, type, attribute, init) \
587  PLDAP_ATTRIBUTE(base, type, attribute, NULL, instance = init;)
588 
589 #define PLDAP_BINATTRIB(base, type, attribute) \
590  PLDAP_ATTRIBUTE(base, type, attribute, &((base &)base::GetInitialiser()).attribute, ;)
591 
592 #define PLDAP_STRUCT_END() \
593  };
594 
595 #endif // P_LDAP
596 
597 #endif // PTLIB_PLDAP_H
598 
599 
600 // End of file ////////////////////////////////////////////////////////////////
AttributeType GetAttributeType(const PString &attribute)
Definition: pldap.h:496
Definition: pldap.h:289
PLDAPAttributeBase * GetAttribute(const char *name) const
Definition: pldap.h:473
void SetSearchLimit(const unsigned s)
Set a limit on the number of results to return.
Definition: pldap.h:416
PString m_name
Definition: pldap.h:152
Definition: pldap.h:122
void SetTimeout(const PTimeInterval &t)
Set the timeout for LDAP operations.
Definition: pldap.h:410
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:51
PArray< PBYTEArray > m_values
Definition: pldap.h:214
PBoolean Modify(const PString &dn, const PArray< ModAttrib > &attributes)
Modify an existing distringuished name to LDAP dirctory.
PBoolean Delete(const PString &dn)
Delete the distinguished name from LDAP directory.
Definition: pldap.h:293
std::map< PString, PBYTEArray > ldapBinAttributes
Definition: pldap.h:542
std::map< PString, PString > ldapAttributes
Definition: pldap.h:541
static PLDAPSchema * CreateSchema(const PString &schemaname, PPluginManager *pluginMgr=NULL)
static PStringArray GetSchemaFriendlyNames(PPluginManager *pluginMgr=NULL)
const PTimeInterval & GetTimeout() const
Get the timeout for LDAP operations.
Definition: pldap.h:406
ldapBinAttributes m_binattributes
Definition: pldap.h:547
virtual PString ToString() const
virtual void FromBinary(const PArray< PBYTEArray > &data)
PBoolean Open(const PString &server, WORD port=0)
Open the LDAP session to the specified server.
AttribDict m_attributes
Definition: pldap.h:481
PStringList GetAttributeList()
virtual PBoolean IsBinary() const
Definition: pldap.h:101
ModAttrib(const PString &name, Operation op=NumOperations)
This is a dictionary collection class of PString objects, keyed by another string.
Definition: pstring.h:3151
AttributeType m_type
Definition: pldap.h:508
PBoolean GetNextSearchResult(SearchContext &context)
Get the next search result.
void SetValue(const PString &value)
void OnReceivedAttribute(const PString &attribute, const PString &value)
static PLDAPStructBase & GetInitialiser()
Definition: pldap.h:476
This class will create an LDAP client to access a remote LDAP server.
Definition: pldap.h:51
Definition: pldap.h:126
void EndConstructor()
StringModAttrib(const PString &name, Operation op=NumOperations)
PLDAPSession(const PString &defaultBaseDN=PString::Empty())
Create a LDAP client.
virtual void AttributeList(attributeList &)
Definition: pldap.h:525
AttribDict & GetAttributes()
Definition: pldap.h:471
SearchScope
Definition: pldap.h:286
This is an array collection class of PString objects.
Definition: pstring.h:2365
PBoolean GetAttribute(const PString &attribute, PString &value)
Definition: pldap.h:497
Definition: pldap.h:99
PString GetErrorText() const
Get the last OpenLDAP error as text string.
const AttribDict & GetAttributes() const
Definition: pldap.h:472
Definition: pldap.h:287
PCREATE_PLUGIN_SERVICE(PLDAPSchema)
This template class maps the PAbstractList to a specific object type.
Definition: lists.h:322
AttributeType
Definition: pldap.h:495
PObject()
Constructor for PObject, made protected so cannot ever create one on its own.
Definition: object.h:2227
Definition: pldap.h:498
void SetBaseDN(const PString &dn)
Set the default base DN for use if not specified for searches.
Definition: pldap.h:384
struct ldap * GetOpenLDAP() const
Get the OpenLDAP context structure.
Definition: pldap.h:402
PString m_defaultBaseDN
Definition: pldap.h:424
unsigned m_protocolVersion
Definition: pldap.h:423
void AddAttribute(PLDAPAttributeBase *var)
Operation m_op
Definition: pldap.h:153
PBoolean Search(SearchContext &context, const PString &filter, const PStringArray &attributes=PStringList(), const PString &base=PString::Empty(), SearchScope scope=ScopeSubTree)
Start search for specified information.
static PStringArray GetSchemaNames(PPluginManager *pluginMgr=NULL)
virtual PBoolean IsBinary() const
Array of unsigned characters.
Definition: array.h:605
PTimeInterval m_timeout
Definition: pldap.h:426
PDictionary< PString, PLDAPAttributeBase > AttribDict
Definition: pldap.h:470
PBoolean Bind(const PString &who=PString::Empty(), const PString &passwd=PString::Empty(), AuthenticationMethod authMethod=AuthSimple)
Bind to the remote LDAP server.
virtual PBYTEArray ToBinary() const
PBoolean SetOption(int optcode, int value)
Set LDAP option parameter (OpenLDAp specific values)
ldapAttributes m_attributes
Definition: pldap.h:546
Definition: pldap.h:499
PString m_multipleValueSeparator
Definition: pldap.h:427
#define PAssertNULL(ptr)
This macro is used to assert that a pointer must be non-null.
Definition: object.h:428
PLDAPStructBase * m_initialiserStack
Definition: pldap.h:483
bool PBoolean
Definition: object.h:174
PBYTEArray m_bervals
Definition: pldap.h:216
virtual void SetLDAPModVars(struct ldapmod &mod)
void AddValue(const PBYTEArray &value)
Definition: pldap.h:490
void * m_pointer
Definition: pldap.h:452
Definition: pldap.h:288
unsigned m_searchLimit
Definition: pldap.h:425
Definition: pldap.h:100
PLDAPAttributeBase(const char *name, void *pointer, PINDEX size)
The character string class.
Definition: pstring.h:108
virtual D * GetAt(const K &key) const
Get the object at the specified key position.
Definition: dict.h:1075
Definition: pldap.h:187
virtual void SetLDAPModVars(struct ldapmod &mod)
PBoolean IsOpen() const
Determine of session is open.
Definition: pldap.h:82
static PMutex m_initialiserMutex
Definition: pldap.h:484
PBoolean StartTLS()
Start encrypted connection.
PString m_name
Definition: pldap.h:507
struct ldap * m_ldapContext
Definition: pldap.h:421
This class defines a thread mutual exclusion object.
Definition: mutex.h:101
PBoolean Add(const PString &dn, const PArray< ModAttrib > &attributes)
Add a new distringuished name to LDAP dirctory.
This is a list collection class of PString objects.
Definition: pstring.h:2562
int GetErrorNumber() const
Get the last OpenLDAP error code.
Definition: pldap.h:394
std::list< Attribute > attributeList
Definition: pldap.h:511
static const PString & Empty()
Return an empty string.
PLDAPStructBase & operator=(const PLDAPStructBase &)
attributeList m_attributelist
Definition: pldap.h:545
const char * GetName() const
Definition: pldap.h:440
Definition: pldap.h:127
Definition: pldap.h:457
void SetValue(const PBYTEArray &value)
static PLDAPStructBase * m_initialiserInstance
Definition: pldap.h:485
PINDEX m_size
Definition: pldap.h:453
const PString & GetBaseDN() const
Set the default base DN for use if not specified for searches.
Definition: pldap.h:390
PStringArray m_values
Definition: pldap.h:183
Operation GetOperation() const
Definition: pldap.h:141
Attribute()
Definition: pldap.h:505
const char * m_name
Definition: pldap.h:451
PBoolean SetAttribute(const PString &attribute, const PString &value)
PBaseArray< const char * > m_pointers
Definition: pldap.h:184
PStringList SchemaName()
Definition: pldap.h:524
int m_errorNumber
Definition: pldap.h:422
Definition: pldap.h:156
Definition: pldap.h:128
const PString & GetName() const
Definition: pldap.h:139
Definition: pldap.h:290
PBoolean Close()
Close the LDAP session.
void AddValue(const PString &value)
void SetLDAPMod(struct ldapmod &mod, Operation defaultOp)
BinaryModAttrib(const PString &name, Operation op=Add)
PBoolean IsCompleted() const
Definition: pldap.h:298
Definition: pldap.h:434
virtual void Copy(const PLDAPAttributeBase &other)=0
virtual void SetLDAPModVars(struct ldapmod &mod)=0
~PLDAPSession()
Close the sesison on destruction.
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
void LoadSchema()
PBoolean Exists(const PString &attribute)
Operation
Definition: pldap.h:125
AuthenticationMethod
Definition: pldap.h:98
void OnSendSchema(PArray< PLDAPSession::ModAttrib > &attributes, PLDAPSession::ModAttrib::Operation op=PLDAPSession::ModAttrib::Add)
PBoolean IsBinary() const
Definition: pldap.h:441
virtual PBoolean IsBinary() const =0
Definition: pldap.h:502
PString GetSearchResultDN(SearchContext &context)
Get the current search result distinguished name entry.
virtual void FromString(const PString &str)
PBoolean GetSearchResult(SearchContext &context, PStringToString &data)
Get the current search result entry.
PBaseArray< struct berval * > m_pointers
Definition: pldap.h:215
void PrintOn(ostream &strm) const
Output the contents of the object to the stream.