PTLib Version 2.12.9

- Main Page
- Related Pages
- 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: 25539 $
27 * $Author: willamowius $
28 * $Date: 2011-04-08 22:26:56 +1000 (Fri, 08 Apr 2011) $
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<const 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 typedef PDictionary<PString, PLDAPAttributeBase> AttribDict;
475 AttribDict & GetAttributes() { return attributes; }
476 const AttribDict & GetAttributes() const { return attributes; }
477 PLDAPAttributeBase * GetAttribute(const char * name) const { return attributes.GetAt(name); }
478
479 void AddAttribute(PLDAPAttributeBase * var);
480 static PLDAPStructBase & GetInitialiser() { return *PAssertNULL(initialiserInstance); }
481
482 protected:
483 void EndConstructor();
484
485 AttribDict attributes;
486
487 PLDAPStructBase * initialiserStack;
488 static PMutex initialiserMutex;
489 static PLDAPStructBase * initialiserInstance;
490 };
491
493
494 class PLDAPSchema : public PObject
495 {
496 public:
497 PLDAPSchema();
498
499 enum AttributeType {
500 AttibuteUnknown = -1,
501 AttributeString,
502 AttributeBinary,
503 AttributeNumeric
504 };
505
506 class Attribute
507 {
508 public:
509 Attribute() : m_type(AttibuteUnknown) { }
510 Attribute(const PString & name, AttributeType type);
511 PString m_name;
512 AttributeType m_type;
513 };
514
515 typedef std::list<Attribute> attributeList;
516
517 static PLDAPSchema * CreateSchema(const PString & schemaname, PPluginManager * pluginMgr = NULL);
518 static PStringList GetSchemaNames(PPluginManager * pluginMgr = NULL);
519 static PStringList GetSchemaFriendlyNames(const PString & schema, PPluginManager * pluginMgr = NULL);
520
521 void OnReceivedAttribute(const PString & attribute, const PString & value);
522
523 void OnSendSchema(PArray<PLDAPSession::ModAttrib> & attributes,
524 PLDAPSession::ModAttrib::Operation op=PLDAPSession::ModAttrib::Add);
525
526 void LoadSchema();
527
528 PStringList SchemaName() { return PStringList(); }
529 virtual void AttributeList(attributeList & /*attrib*/) {};
530
531
532 PStringList GetAttributeList();
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
549 attributeList attributelist;
550 ldapAttributes attributes;
551 ldapBinAttributes binattributes;
552 };
553
554
555 template <class className> class LDAPPluginServiceDescriptor : public PDevicePluginServiceDescriptor
556 {
557 public:
558 virtual PObject * CreateInstance(int /*userData*/) const { return new className; }
559 virtual PStringArray GetDeviceNames(int /*userData*/) const { return className::SchemaName(); }
560 };
561
562 #define LDAP_Schema(name) \
563 static LDAPPluginServiceDescriptor<name##_schema> name##_schema_descriptor; \
564 PCREATE_PLUGIN(name##_schema, PLDAPSchema, &name##_schema_descriptor)
565
567
568 #define PLDAP_STRUCT_BEGIN(name) \
569 class name : public PLDAPStructBase { \
570 public: name() : PLDAPStructBase() { EndConstructor(); } \
571 public: name(const name & other) : PLDAPStructBase() { EndConstructor(); operator=(other); } \
572 public: name(const PStringArray & array) : PLDAPStructBase() { EndConstructor(); operator=(array); } \
573 public: name(const PStringToString & dict) : PLDAPStructBase() { EndConstructor(); operator=(dict); } \
574 public: name & operator=(const name & other) { PLDAPStructBase::operator=(other); return *this; } \
575 public: name & operator=(const PStringArray & array) { PLDAPStructBase::operator=(array); return *this; } \
576 public: name & operator=(const PStringToString & dict) { PLDAPStructBase::operator=(dict); return *this; } \
577 PLDAP_ATTR_INIT(name, PString, objectClass, #name);
578
579 #define PLDAP_ATTRIBUTE(base, type, attribute, pointer, init) \
580 public: type attribute; \
581 private: struct PLDAPAttr_##attribute : public PLDAPAttributeBase { \
582 PLDAPAttr_##attribute() \
583 : PLDAPAttributeBase(#attribute, pointer, sizeof(type)), \
584 instance(((base &)base::GetInitialiser()).attribute) \
585 { init } \
586 virtual void PrintOn (ostream & s) const { s << instance; } \
587 virtual void ReadFrom(istream & s) { s >> instance; } \
588 virtual void Copy(const PLDAPAttributeBase & other) \
589 { instance = ((PLDAPAttr_##attribute &)other).instance; } \
590 type & instance; \
591 } pldapvar_##attribute
592
593 #define PLDAP_ATTR_SIMP(base, type, attribute) \
594 PLDAP_ATTRIBUTE(base, type, attribute, NULL, ;)
595
596 #define PLDAP_ATTR_INIT(base, type, attribute, init) \
597 PLDAP_ATTRIBUTE(base, type, attribute, NULL, instance = init;)
598
599 #define PLDAP_BINATTRIB(base, type, attribute) \
600 PLDAP_ATTRIBUTE(base, type, attribute, &((base &)base::GetInitialiser()).attribute, ;)
601
602 #define PLDAP_STRUCT_END() \
603 };
604
605 #endif // P_LDAP
606
607 #endif // PTLIB_PLDAP_H
608
609
610 // End of file ////////////////////////////////////////////////////////////////