Added a parameter to semaphore constructor to avoid ambiguity
[pwlib.git] / samples / ldaptest / main.cxx
blob831360284f6ff21f09bb4e103af857517367c3aa
1 /*
2 * main.cxx
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
18 * under the License.
20 * The Original Code is Portable Windows Library.
22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
24 * Contributor(s): ______________________________________.
26 * $Log$
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
35 * OpenLDAP support.
39 #include "precompile.h"
40 #include "main.h"
41 #include "version.h"
43 #include <ptclib/pldap.h>
44 #include <ptclib/pils.h>
46 #if !P_LDAP
47 #error Must have LDAP enabled for this application.
48 #endif
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);
68 LDAPTest::LDAPTest()
69 : PProcess("Equivalence", "LDAP Test", MAJOR_VERSION, MINOR_VERSION, BUILD_TYPE, BUILD_NUMBER)
74 void LDAPTest::Main()
76 PArgList & args = GetArguments();
77 args.Parse("h:p:x.b:s:P.I.", FALSE);
79 if (args.GetCount() == 0) {
80 Usage();
81 return;
84 if (args.HasOption('I')) {
85 PILSSession ils;
86 if (!ils.Open(args.GetOptionString('h'), (WORD)args.GetOptionString('p').AsUnsigned())) {
87 cerr << "Could not open ILS server at " << args[1];
88 return;
91 if (args[0] *= "add") {
92 AddILS(args, ils);
93 return;
96 if (args[0] *= "delete") {
97 DeleteILS(args, ils);
98 return;
101 if (args[0] *= "search") {
102 SearchILS(args, ils);
103 return;
106 else {
107 PLDAPSession ldap;
108 if (!ldap.Open(args.GetOptionString('h'), (WORD)args.GetOptionString('p').AsUnsigned())) {
109 cerr << "Could not open LDAP server at " << args[1];
110 return;
113 if (args[0] *= "add") {
114 Add(args, ldap);
115 return;
118 if (args[0] *= "delete") {
119 Delete(args, ldap);
120 return;
123 if (args[0] *= "search") {
124 Search(args, ldap);
125 return;
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"
142 "\n"
143 " add arguments:\n"
144 " dn attribute=value [ attribute=value ... ]\n"
145 " Note if -I is used then dn is actually the cn.\n"
146 "\n"
147 " delete arguments:\n"
148 " dn [ dn ... ]\n"
149 " Note if -I is used then dn is actually the cn.\n"
150 "\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"
156 "\n";
160 void LDAPTest::Add(PArgList & args, PLDAPSession & ldap)
162 if (args.GetCount() < 3) {
163 Usage();
164 return;
167 if (ldap.Add(args[1], args.GetParameters(2)))
168 cout << "Added " << args[1] << endl;
169 else
170 cout << "Could not add " << args[1] << ": " << ldap.GetErrorText() << endl;
174 void LDAPTest::Delete(PArgList & args, PLDAPSession & ldap)
176 if (args.GetCount() < 2) {
177 Usage();
178 return;
181 for (PINDEX i = 1; i < args.GetCount(); i++) {
182 if (ldap.Delete(args[i]))
183 cout << args[i] << " removed." << endl;
184 else
185 cout << "Could not remove " << args[i] << ": " << ldap.GetErrorText() << endl;
190 void LDAPTest::Search(PArgList & args, PLDAPSession & ldap)
192 if (args.GetCount() < 2) {
193 Usage();
194 return;
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))) {
202 do {
203 PStringToString entry;
204 if (ldap.GetSearchResult(context, entry)) {
205 cout << entry << "\n\nPress ENTER ..." << flush;
206 cin.get();
207 cout << '\n';
209 } while (ldap.GetNextSearchResult(context));
211 else
212 cout << "Could not find entries for filter: " << args[1]
213 << ": " << ldap.GetErrorText() << endl;
215 else {
216 PList<PStringToString> data = ldap.Search(args[1], args.GetParameters(2));
217 if (data.IsEmpty())
218 cout << "Could not find entries for filter: " << args[1]
219 << ": " << ldap.GetErrorText() << endl;
220 else
221 cout << setfill('\n') << data;
226 void LDAPTest::AddILS(PArgList & args, PILSSession & ils)
228 if (args.GetCount() < 2) {
229 Usage();
230 return;
233 PILSSession::RTPerson person = args.GetParameters(2);
234 person.cn = args[1];
235 if (person.rfc822Mailbox.IsEmpty())
236 person.rfc822Mailbox = person.cn;
238 if (ils.AddPerson(person))
239 cout << person.GetDN() << " added." << endl;
240 else
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) {
248 Usage();
249 return;
252 PILSSession::RTPerson person;
253 person.cn = args[1];
254 if (ils.DeletePerson(person))
255 cout << args[1] << " removed." << endl;
256 else
257 cout << "Could not remove person: " << args[1] << ": " << ils.GetErrorText() << endl;
261 void LDAPTest::SearchILS(PArgList & args, PILSSession & ils)
263 if (args.GetCount() < 2) {
264 Usage();
265 return;
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)) {
275 do {
276 PILSSession::RTPerson person;
277 if (ils.GetSearchResult(context, person)) {
278 cout << person << "\n\nPress ENTER ..." << flush;
279 cin.get();
280 cout << '\n';
282 } while (ils.GetNextSearchResult(context));
284 else
285 cout << "Could not find people for filter: " << filter
286 << ": " << ils.GetErrorText() << endl;
288 else {
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;
293 else
294 cout << setfill('\n') << people;
299 // End of File ///////////////////////////////////////////////////////////////