4 * PWLib application source file for LDAP Test
6 * Main program entry point.
8 * Copyright (c) 2003 Equivalence Pty. Ltd.
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/
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
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Contributor(s): ______________________________________.
27 * Revision 1.3 2003/09/26 13:43:49 rjongbloed
28 * Added special test to give more indicative error if try to compile without LDAP support.
30 * Revision 1.2 2003/03/31 03:35:20 robertj
31 * Major addition of LDAP functionality.
32 * Added ILS specialisation of LDAP.
34 * Revision 1.1 2003/03/28 01:15:44 robertj
39 #include "precompile.h"
43 #include <ptclib/pldap.h>
44 #include <ptclib/pils.h>
47 #error Must have LDAP enabled for this application.
51 /* Test command lines:
53 add -h ils.seconix.com -x "c=AU, cn=robertj@equival.com.au, objectClass=RTPerson" "cn=robertj@equival.com.au" surname=Jongbloed givenName=Robert c=AU
54 delete -h ils.seconix.com -x "c=AU, cn=robertj@equival.com.au, objectClass=RTPerson"
55 search -h ils.seconix.com -x -b "objectClass=RTPerson" "cn=*au"
56 search -h ils.seconix.com -x -b "objectClass=RTPerson" "cn=*" cn surname givenName -P
58 add -h ils.seconix.com -x -I robertj@equival.com.au surname=Jongbloed givenName=Robert c=AU rfc822Mailbox=robertj@equival.com.au
59 delete -h ils.seconix.com -x -I robertj@equival.com.au
60 search -h ils.seconix.com -x -I "*" -P
64 PCREATE_PROCESS(LDAPTest
);
69 : PProcess("Equivalence", "LDAP Test", MAJOR_VERSION
, MINOR_VERSION
, BUILD_TYPE
, BUILD_NUMBER
)
76 PArgList
& args
= GetArguments();
77 args
.Parse("h:p:x.b:s:P.I.", FALSE
);
79 if (args
.GetCount() == 0) {
84 if (args
.HasOption('I')) {
86 if (!ils
.Open(args
.GetOptionString('h'), (WORD
)args
.GetOptionString('p').AsUnsigned())) {
87 cerr
<< "Could not open ILS server at " << args
[1];
91 if (args
[0] *= "add") {
96 if (args
[0] *= "delete") {
101 if (args
[0] *= "search") {
102 SearchILS(args
, ils
);
108 if (!ldap
.Open(args
.GetOptionString('h'), (WORD
)args
.GetOptionString('p').AsUnsigned())) {
109 cerr
<< "Could not open LDAP server at " << args
[1];
113 if (args
[0] *= "add") {
118 if (args
[0] *= "delete") {
123 if (args
[0] *= "search") {
129 cerr
<< "Invalid command: " << args
[0];
133 void LDAPTest::Usage()
135 cerr
<< "usage: " << GetFile().GetTitle() << " { add | delete | search } [ args ]\n"
136 " General arguments:\n"
137 " -h host LDAP server\n"
138 " -p port port on LDAP server\n"
139 " -s scope one of base, one, or sub (search scope)\n"
140 " -x Simple authentication\n"
141 " -I use ILS schema\n"
144 " dn attribute=value [ attribute=value ... ]\n"
145 " Note if -I is used then dn is actually the cn.\n"
147 " delete arguments:\n"
149 " Note if -I is used then dn is actually the cn.\n"
151 " search arguments:\n"
152 " filter [ attribute ... ]\n"
153 " -b basedn base dn for search\n"
154 " -P Pause between entries\n"
155 " Note if -I is used then filter is implicitly cn=filter.\n"
160 void LDAPTest::Add(PArgList
& args
, PLDAPSession
& ldap
)
162 if (args
.GetCount() < 3) {
167 if (ldap
.Add(args
[1], args
.GetParameters(2)))
168 cout
<< "Added " << args
[1] << endl
;
170 cout
<< "Could not add " << args
[1] << ": " << ldap
.GetErrorText() << endl
;
174 void LDAPTest::Delete(PArgList
& args
, PLDAPSession
& ldap
)
176 if (args
.GetCount() < 2) {
181 for (PINDEX i
= 1; i
< args
.GetCount(); i
++) {
182 if (ldap
.Delete(args
[i
]))
183 cout
<< args
[i
] << " removed." << endl
;
185 cout
<< "Could not remove " << args
[i
] << ": " << ldap
.GetErrorText() << endl
;
190 void LDAPTest::Search(PArgList
& args
, PLDAPSession
& ldap
)
192 if (args
.GetCount() < 2) {
197 ldap
.SetBaseDN(args
.GetOptionString('b'));
199 if (args
.HasOption('P')) {
200 PLDAPSession::SearchContext context
;
201 if (ldap
.Search(context
, args
[1], args
.GetParameters(2))) {
203 PStringToString entry
;
204 if (ldap
.GetSearchResult(context
, entry
)) {
205 cout
<< entry
<< "\n\nPress ENTER ..." << flush
;
209 } while (ldap
.GetNextSearchResult(context
));
212 cout
<< "Could not find entries for filter: " << args
[1]
213 << ": " << ldap
.GetErrorText() << endl
;
216 PList
<PStringToString
> data
= ldap
.Search(args
[1], args
.GetParameters(2));
218 cout
<< "Could not find entries for filter: " << args
[1]
219 << ": " << ldap
.GetErrorText() << endl
;
221 cout
<< setfill('\n') << data
;
226 void LDAPTest::AddILS(PArgList
& args
, PILSSession
& ils
)
228 if (args
.GetCount() < 2) {
233 PILSSession::RTPerson person
= args
.GetParameters(2);
235 if (person
.rfc822Mailbox
.IsEmpty())
236 person
.rfc822Mailbox
= person
.cn
;
238 if (ils
.AddPerson(person
))
239 cout
<< person
.GetDN() << " added." << endl
;
241 cout
<< "Could not add person:\n" << person
<< "\nError: " << ils
.GetErrorText() << endl
;
245 void LDAPTest::DeleteILS(PArgList
& args
, PILSSession
& ils
)
247 if (args
.GetCount() < 1) {
252 PILSSession::RTPerson person
;
254 if (ils
.DeletePerson(person
))
255 cout
<< args
[1] << " removed." << endl
;
257 cout
<< "Could not remove person: " << args
[1] << ": " << ils
.GetErrorText() << endl
;
261 void LDAPTest::SearchILS(PArgList
& args
, PILSSession
& ils
)
263 if (args
.GetCount() < 2) {
268 PString filter
= args
[1];
269 if (filter
.Find('=') != P_MAX_INDEX
)
270 filter
.Splice("cn=", 0);
272 if (args
.HasOption('P')) {
273 PLDAPSession::SearchContext context
;
274 if (ils
.Search(context
, filter
)) {
276 PILSSession::RTPerson person
;
277 if (ils
.GetSearchResult(context
, person
)) {
278 cout
<< person
<< "\n\nPress ENTER ..." << flush
;
282 } while (ils
.GetNextSearchResult(context
));
285 cout
<< "Could not find people for filter: " << filter
286 << ": " << ils
.GetErrorText() << endl
;
289 PList
<PILSSession::RTPerson
> people
= ils
.SearchPeople(filter
);
290 if (people
.IsEmpty())
291 cout
<< "Could not find people for filter: " << filter
292 << ": " << ils
.GetErrorText() << endl
;
294 cout
<< setfill('\n') << people
;
299 // End of File ///////////////////////////////////////////////////////////////