PTLib  Version 2.14.3
 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: 30779 $
27  * $Author: rjongbloed $
28  * $Date: 2013-10-29 18:10:28 +1100 (Tue, 29 Oct 2013) $
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:
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 
91  int optcode,
92  int value
93  );
94 
98  int optcode,
99  void * value
100  );
101 
106 #ifdef SOLARIS
107  NumAuthenticationMethod1,
108  NumAuthenticationMethod2
109 #else
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 {
134  };
135 
136  protected:
137  ModAttrib(
138  const PString & name,
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 
158  };
159 
160  class StringModAttrib : public ModAttrib {
161  PCLASSINFO(StringModAttrib, ModAttrib);
162  public:
164  const PString & name,
166  );
168  const PString & name,
169  const PString & value,
171  );
173  const PString & name,
174  const PStringList & values,
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 
189  };
190 
191  class BinaryModAttrib : public ModAttrib {
192  PCLASSINFO(BinaryModAttrib, ModAttrib);
193  public:
195  const PString & name,
196  Operation op = Add
197  );
199  const PString & name,
200  const PBYTEArray & value,
201  Operation op = Add
202  );
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 
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 
256  const PString & dn,
257  const PArray<ModAttrib> & attributes
258  );
259 
263  const PString & dn,
264  const PStringToString & attributes
265  );
266 
271  const PString & dn,
272  const PStringArray & attributes
273  );
274 
279  const PString & dn,
280  const PLDAPStructBase & data
281  );
282 
286  const PString & dn
287  );
288 
289 
290  enum SearchScope {
295  };
296 
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 
317  SearchContext & context,
318  const PString & filter,
319  const PStringArray & attributes = PStringList(),
320  const PString & base = PString::Empty(),
321  SearchScope scope = ScopeSubTree
322  );
323 
327  SearchContext & context,
328  PStringToString & data
329  );
330 
334  SearchContext & context,
335  const PString & attribute,
336  PString & data
337  );
338 
342  SearchContext & context,
343  const PString & attribute,
344  PStringArray & data
345  );
346 
350  SearchContext & context,
351  const PString & attribute,
352  PArray<PBYTEArray> & data
353  );
354 
358  SearchContext & context,
359  PLDAPStructBase & data
360  );
361 
365  SearchContext & context
366  );
367 
371  SearchContext & context
372  );
373 
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 
415  const PTimeInterval & t
416  ) { timeout = t; }
417 
421  const unsigned s
422  ) { searchLimit = s; }
423 
424  protected:
425  struct ldap * ldapContext;
427  unsigned protocolVersion;
429  unsigned searchLimit;
432 };
433 
434 
435 
436 class PLDAPStructBase;
437 
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();
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 
476  const AttribDict & GetAttributes() const { return attributes; }
477  PLDAPAttributeBase * GetAttribute(const char * name) const { return attributes.GetAt(name); }
478 
479  void AddAttribute(PLDAPAttributeBase * var);
481 
482  protected:
483  void EndConstructor();
484 
486 
490 };
491 
493 
494 class PLDAPSchema : public PObject
495 {
496  public:
497  PLDAPSchema();
498 
504  };
505 
506  class Attribute
507  {
508  public:
510  Attribute(const PString & name, AttributeType type);
513  };
514 
515  typedef std::list<Attribute> attributeList;
516 
517  static PLDAPSchema * CreateSchema(const PString & schemaname, PPluginManager * pluginMgr = NULL);
518  static PStringArray GetSchemaNames(PPluginManager * pluginMgr = NULL);
519  static PStringArray GetSchemaFriendlyNames(PPluginManager * pluginMgr = NULL);
520 
521  void OnReceivedAttribute(const PString & attribute, const PString & value);
522 
525 
526  void LoadSchema();
527 
529  virtual void AttributeList(attributeList & /*attrib*/) {};
530 
531 
533  PBoolean Exists(const PString & attribute);
534 
535  PBoolean SetAttribute(const PString & attribute, const PString & value);
536  PBoolean SetAttribute(const PString & attribute, const PBYTEArray & value);
537 
538  PBoolean GetAttribute(const PString & attribute, PString & value);
539  PBoolean GetAttribute(const PString & attribute, PBYTEArray & value);
540 
541  AttributeType GetAttributeType(const PString & attribute);
542 
543 
544  protected:
545  typedef std::map<PString,PString> ldapAttributes;
546  typedef std::map<PString,PBYTEArray> ldapBinAttributes;
547 
548 
552 };
553 
554 
556 
557 #define LDAP_Schema(name) PCREATE_PLUGIN(name, PLDAPSchema)
558 
559 
561 
562 #define PLDAP_STRUCT_BEGIN(name) \
563  class name : public PLDAPStructBase { \
564  public: name() : PLDAPStructBase() { EndConstructor(); } \
565  public: name(const name & other) : PLDAPStructBase() { EndConstructor(); operator=(other); } \
566  public: name(const PStringArray & array) : PLDAPStructBase() { EndConstructor(); operator=(array); } \
567  public: name(const PStringToString & dict) : PLDAPStructBase() { EndConstructor(); operator=(dict); } \
568  public: name & operator=(const name & other) { PLDAPStructBase::operator=(other); return *this; } \
569  public: name & operator=(const PStringArray & array) { PLDAPStructBase::operator=(array); return *this; } \
570  public: name & operator=(const PStringToString & dict) { PLDAPStructBase::operator=(dict); return *this; } \
571  PLDAP_ATTR_INIT(name, PString, objectClass, #name);
572 
573 #define PLDAP_ATTRIBUTE(base, type, attribute, pointer, init) \
574  public: type attribute; \
575  private: struct PLDAPAttr_##attribute : public PLDAPAttributeBase { \
576  PLDAPAttr_##attribute() \
577  : PLDAPAttributeBase(#attribute, pointer, sizeof(type)), \
578  instance(((base &)base::GetInitialiser()).attribute) \
579  { init } \
580  virtual void PrintOn (ostream & s) const { s << instance; } \
581  virtual void ReadFrom(istream & s) { s >> instance; } \
582  virtual void Copy(const PLDAPAttributeBase & other) \
583  { instance = ((PLDAPAttr_##attribute &)other).instance; } \
584  type & instance; \
585  } pldapvar_##attribute
586 
587 #define PLDAP_ATTR_SIMP(base, type, attribute) \
588  PLDAP_ATTRIBUTE(base, type, attribute, NULL, ;)
589 
590 #define PLDAP_ATTR_INIT(base, type, attribute, init) \
591  PLDAP_ATTRIBUTE(base, type, attribute, NULL, instance = init;)
592 
593 #define PLDAP_BINATTRIB(base, type, attribute) \
594  PLDAP_ATTRIBUTE(base, type, attribute, &((base &)base::GetInitialiser()).attribute, ;)
595 
596 #define PLDAP_STRUCT_END() \
597  };
598 
599 #endif // P_LDAP
600 
601 #endif // PTLIB_PLDAP_H
602 
603 
604 // End of file ////////////////////////////////////////////////////////////////