PTLib  Version 2.10.4
 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: 22443 $
27  * $Author: rjongbloed $
28  * $Date: 2009-04-20 18:47:22 -0500 (Mon, 20 Apr 2009) $
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 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 name; }
140 
141  Operation GetOperation() const { return 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 completed; }
299 
300  private:
301  int msgid;
302  struct ldapmsg * result;
303  struct ldapmsg * message;
304  PBoolean found;
305  PBoolean 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  ) { defaultBaseDN = dn; }
387 
390  const PString & GetBaseDN() const { return defaultBaseDN; }
391 
394  int GetErrorNumber() const { return errorNumber; }
395 
398  PString GetErrorText() const;
399 
402  struct ldap * GetOpenLDAP() const { return ldapContext; }
403 
406  const PTimeInterval & GetTimeout() const { return timeout; }
407 
411  const PTimeInterval & t
412  ) { timeout = t; }
413 
417  const unsigned s
418  ) { searchLimit = s; }
419 
420  protected:
421  struct ldap * ldapContext;
423  unsigned protocolVersion;
425  unsigned 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 name; }
441  PBoolean IsBinary() const { return 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 * name;
452  void * pointer;
453  PINDEX 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 
470  PINDEX GetNumAttributes() const { return attributes.GetSize(); }
471  PLDAPAttributeBase & GetAttribute(PINDEX idx) const { return attributes.GetDataAt(idx); }
472  PLDAPAttributeBase * GetAttribute(const char * name) const { return attributes.GetAt(name); }
473 
474  void AddAttribute(PLDAPAttributeBase * var);
476 
477  protected:
478  void EndConstructor();
479 
481 
485 };
486 
488 
489 class PLDAPSchema : public PObject
490 {
491  public:
492  PLDAPSchema();
493 
499  };
500 
501  class Attribute
502  {
503  public:
505  Attribute(const PString & name, AttributeType type);
508  };
509 
510  typedef std::list<Attribute> attributeList;
511 
512  static PLDAPSchema * CreateSchema(const PString & schemaname, PPluginManager * pluginMgr = NULL);
513  static PStringList GetSchemaNames(PPluginManager * pluginMgr = NULL);
514  static PStringList GetSchemaFriendlyNames(const PString & schema, PPluginManager * pluginMgr = NULL);
515 
516  void OnReceivedAttribute(const PString & attribute, const PString & value);
517 
520 
521  void LoadSchema();
522 
524  virtual void AttributeList(attributeList & /*attrib*/) {};
525 
526 
528  PBoolean Exists(const PString & attribute);
529 
530  PBoolean SetAttribute(const PString & attribute, const PString & value);
531  PBoolean SetAttribute(const PString & attribute, const PBYTEArray & value);
532 
533  PBoolean GetAttribute(const PString & attribute, PString & value);
534  PBoolean GetAttribute(const PString & attribute, PBYTEArray & value);
535 
536  AttributeType GetAttributeType(const PString & attribute);
537 
538 
539  protected:
540  typedef std::map<PString,PString> ldapAttributes;
541  typedef std::map<PString,PBYTEArray> ldapBinAttributes;
542 
543 
547 };
548 
549 
550 template <class className> class LDAPPluginServiceDescriptor : public PDevicePluginServiceDescriptor
551 {
552  public:
553  virtual PObject * CreateInstance(int /*userData*/) const { return new className; }
554  virtual PStringArray GetDeviceNames(int /*userData*/) const { return className::SchemaName(); }
555 };
556 
557 #define LDAP_Schema(name) \
558  static LDAPPluginServiceDescriptor<name##_schema> name##_schema_descriptor; \
559  PCREATE_PLUGIN(name##_schema, PLDAPSchema, &name##_schema_descriptor)
560 
562 
563 #define PLDAP_STRUCT_BEGIN(name) \
564  class name : public PLDAPStructBase { \
565  public: name() : PLDAPStructBase() { EndConstructor(); } \
566  public: name(const name & other) : PLDAPStructBase() { EndConstructor(); operator=(other); } \
567  public: name(const PStringArray & array) : PLDAPStructBase() { EndConstructor(); operator=(array); } \
568  public: name(const PStringToString & dict) : PLDAPStructBase() { EndConstructor(); operator=(dict); } \
569  public: name & operator=(const name & other) { PLDAPStructBase::operator=(other); return *this; } \
570  public: name & operator=(const PStringArray & array) { PLDAPStructBase::operator=(array); return *this; } \
571  public: name & operator=(const PStringToString & dict) { PLDAPStructBase::operator=(dict); return *this; } \
572  PLDAP_ATTR_INIT(name, PString, objectClass, #name);
573 
574 #define PLDAP_ATTRIBUTE(base, type, attribute, pointer, init) \
575  public: type attribute; \
576  private: struct PLDAPAttr_##attribute : public PLDAPAttributeBase { \
577  PLDAPAttr_##attribute() \
578  : PLDAPAttributeBase(#attribute, pointer, sizeof(type)), \
579  instance(((base &)base::GetInitialiser()).attribute) \
580  { init } \
581  virtual void PrintOn (ostream & s) const { s << instance; } \
582  virtual void ReadFrom(istream & s) { s >> instance; } \
583  virtual void Copy(const PLDAPAttributeBase & other) \
584  { instance = ((PLDAPAttr_##attribute &)other).instance; } \
585  type & instance; \
586  } pldapvar_##attribute
587 
588 #define PLDAP_ATTR_SIMP(base, type, attribute) \
589  PLDAP_ATTRIBUTE(base, type, attribute, NULL, ;)
590 
591 #define PLDAP_ATTR_INIT(base, type, attribute, init) \
592  PLDAP_ATTRIBUTE(base, type, attribute, NULL, instance = init;)
593 
594 #define PLDAP_BINATTRIB(base, type, attribute) \
595  PLDAP_ATTRIBUTE(base, type, attribute, &((base &)base::GetInitialiser()).attribute, ;)
596 
597 #define PLDAP_STRUCT_END() \
598  };
599 
600 #endif // P_LDAP
601 
602 #endif // PTLIB_PLDAP_H
603 
604 
605 // End of file ////////////////////////////////////////////////////////////////