Skip to content

Commit 8c28bbd

Browse files
committed
Fix a bunch of the error messages for the LDAP Server.
1 parent bca87c6 commit 8c28bbd

File tree

6 files changed

+93
-50
lines changed

6 files changed

+93
-50
lines changed

pom.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,20 @@
8383
<executions>
8484
<execution>
8585
<id>ldap-server</id>
86-
<phase>compile</phase>
86+
<phase>package</phase>
8787
<goals>
8888
<goal>run</goal>
8989
</goals>
9090
<configuration>
9191
<target>
9292
<ant target="run" antfile="${basedir}/src/config/build.xml">
93-
<!-- This is the important bit -->
9493
<reference torefid="maven.compile.classpath" refid="maven.compile.classpath" />
9594
</ant>
9695
</target>
9796
</configuration>
9897
</execution>
9998
</executions>
10099
</plugin>
101-
102100
<plugin>
103101
<groupId>org.codehaus.cargo</groupId>
104102
<artifactId>cargo-maven2-plugin</artifactId>
@@ -549,6 +547,7 @@
549547
</execution>
550548
</executions>
551549
</plugin>
550+
552551
<!-- FindBugs Static Analysis -->
553552
<plugin>
554553
<groupId>org.codehaus.mojo</groupId>

runBenchmark.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
call mvn compile verify cargo:run -Pdeploy
1+
call mvn clean package cargo:run -Pdeploy

runBenchmark.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/sh
22

33
chmod 755 src/main/resources/insecureCmd.sh
4-
mvn clean compile verify cargo:run -Pdeploy
4+
mvn clean package cargo:run -Pdeploy

src/main/java/org/owasp/benchmark/helpers/LDAPManager.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,6 @@ protected Hashtable<Object, Object> createEnv() {
8080
}
8181

8282
public boolean insert(LDAPPerson person) {
83-
/*
84-
DirContext ctx = null;
85-
try {
86-
ctx = getDirContext();
87-
} catch (NamingException e1) {
88-
System.out.println("Problem getting LDAP - DirContext:" + e1.getMessage());
89-
}
90-
*/
9183
Attributes matchAttrs = new BasicAttributes(true);
9284
matchAttrs.put(new BasicAttribute("uid", person.getName()));
9385
matchAttrs.put(new BasicAttribute("cn", person.getName()));
@@ -104,7 +96,9 @@ public boolean insert(LDAPPerson person) {
10496
try {
10597
iniDirContext.bind(name, ctx, matchAttrs);
10698
} catch (NamingException e) {
107-
System.out.println("Record already exist or an error ocurred: " + e.getMessage());
99+
if(!e.getMessage().contains("ENTRY_ALREADY_EXISTS")){
100+
System.out.println("Record already exist or an error ocurred: " + e.getMessage());
101+
}
108102
}finally{
109103
if(iniDirContext != null){
110104
/*
@@ -117,8 +111,7 @@ public boolean insert(LDAPPerson person) {
117111
}
118112
}
119113

120-
// logger.debug("success inserting " + person.getName());
121-
System.out.println("inserted");
114+
// System.out.println("inserted");
122115
return true;
123116

124117
}
@@ -151,13 +144,14 @@ private boolean search(LDAPPerson person) {
151144
Attribute attr = attrs.get("uid");
152145
if (attr != null) {
153146
// logger.debug("record found " + attr.get());
154-
System.out.println("record found " + attr.get());
147+
// System.out.println("record found " + attr.get());
155148
}
156149
}
157150
ctx.close();
158151

159152
return true;
160153
} catch (Exception e) {
154+
System.out.println("LDAP error search: ");
161155
// logger.error(e, e);
162156
e.printStackTrace();
163157
return false;

src/main/java/org/owasp/benchmark/helpers/LDAPServer.java

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class LDAPServer {
3636
public LDAPServer() {
3737
String dir = Utils.getFileFromClasspath("benchmark.properties", LDAPManager.class.getClassLoader()).getParent();
3838
File workDir = new File(dir + "/../ldap");
39-
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%% " + dir + "/../ldap");
39+
// System.out.println(dir + "/../ldap");
4040
// File workDir = new File(System.getProperty("user.dir") +
4141
workDir.mkdirs();
4242

@@ -56,7 +56,7 @@ public LDAPServer() {
5656
}
5757

5858
// And print it if available
59-
System.out.println("Found entry : " + result);
59+
// System.out.println("Found entry : " + result);
6060

6161
// optionally we can start a server too
6262
try {
@@ -97,17 +97,26 @@ public LDAPServer() {
9797
* @throws Exception
9898
* if there were some problems while initializing the system
9999
*/
100-
private void initDirectoryService(File workDir) throws Exception {
100+
private void initDirectoryService(File workDir){
101101
// Initialize the LDAP service
102-
service = new DefaultDirectoryService();
102+
try {
103+
service = new DefaultDirectoryService();
104+
} catch (Exception e1) {
105+
System.out.println("Error creating DefaultDirectoryService. " + e1.getMessage());
106+
}
103107
service.setWorkingDirectory(workDir);
104108

105109
// first load the schema
106110
initSchemaPartition();
107111

108112
// then the system partition
109113
// this is a MANDATORY partition
110-
Partition systemPartition = addPartition("system", ServerDNConstants.SYSTEM_DN);
114+
Partition systemPartition = null;
115+
try {
116+
systemPartition = addPartition("system", ServerDNConstants.SYSTEM_DN);
117+
} catch (Exception e1) {
118+
System.out.println("Error addPartition system. " + e1.getMessage());
119+
}
111120
service.setSystemPartition(systemPartition);
112121

113122
// Disable the ChangeLog system
@@ -116,51 +125,80 @@ private void initDirectoryService(File workDir) throws Exception {
116125

117126
// Now we can create as many partitions as we need
118127
// Create some new partitions named 'foo', 'bar' and 'apache'.
119-
Partition fooPartition = addPartition("foo", "dc=foo,dc=com");
120-
Partition barPartition = addPartition("bar", "dc=bar,dc=com");
121-
Partition apachePartition = addPartition("apache", "dc=apache,dc=org");
128+
Partition fooPartition = null;
129+
try {
130+
fooPartition = addPartition("foo", "dc=foo,dc=com");
131+
} catch (Exception e1) {
132+
System.out.println("Error addPartition foo. " + e1.getMessage());
133+
}
134+
Partition barPartition = null;
135+
try {
136+
barPartition = addPartition("bar", "dc=bar,dc=com");
137+
} catch (Exception e1) {
138+
System.out.println("Error addPartition bar. " + e1.getMessage());
139+
}
140+
Partition apachePartition = null;
141+
try {
142+
apachePartition = addPartition("apache", "dc=apache,dc=org");
143+
} catch (Exception e1) {
144+
System.out.println("Error addPartition apache. " + e1.getMessage());
145+
}
122146

123147
// Index some attributes on the apache partition
124148
addIndex(apachePartition, "objectClass", "ou", "uid");
125149
try {
126150
// And start the service
127151
service.startup();
128152
} catch (Exception e) {
129-
e.printStackTrace();
130-
//System.out.println("%%%%%%%ERROR%%%%%%%%" + e.getMessage());
153+
System.out.println("Error at LDAP startup: " + e.getMessage());
131154
}
132155
// Inject the foo root entry if it does not already exist
133156
try {
134157
service.getAdminSession().lookup(fooPartition.getSuffixDn());
135-
} catch (LdapException lnnfe) {
136-
DN dnFoo = new DN("dc=foo,dc=com");
137-
ServerEntry entryFoo = service.newEntry(dnFoo);
138-
entryFoo.add("objectClass", "top", "domain", "extensibleObject");
139-
entryFoo.add("dc", "foo");
140-
service.getAdminSession().add(entryFoo);
158+
} catch (Exception lnnfe) {
159+
try {
160+
DN dnFoo = new DN("dc=foo,dc=com");
161+
ServerEntry entryFoo = service.newEntry(dnFoo);
162+
entryFoo.add("objectClass", "top", "domain", "extensibleObject");
163+
entryFoo.add("dc", "foo");
164+
service.getAdminSession().add(entryFoo);
165+
} catch (Exception e) {
166+
System.out.println("Error creating new DN.");
167+
}
141168
}
142169

143170
// Inject the bar root entry
144171
try {
145172
service.getAdminSession().lookup(barPartition.getSuffixDn());
146-
} catch (LdapException lnnfe) {
147-
DN dnBar = new DN("dc=bar,dc=com");
148-
ServerEntry entryBar = service.newEntry(dnBar);
149-
entryBar.add("objectClass", "top", "domain", "extensibleObject");
150-
entryBar.add("dc", "bar");
151-
service.getAdminSession().add(entryBar);
173+
} catch (Exception lnnfe) {
174+
try {
175+
DN dnBar = new DN("dc=bar,dc=com");
176+
ServerEntry entryBar = service.newEntry(dnBar);
177+
entryBar.add("objectClass", "top", "domain", "extensibleObject");
178+
entryBar.add("dc", "bar");
179+
service.getAdminSession().add(entryBar);
180+
} catch (Exception e) {
181+
System.out.println("Error creating new DN.");
182+
}
152183
}
153184

154185
// Inject the apache root entry
155-
if (!service.getAdminSession().exists(apachePartition.getSuffixDn())) {
156-
DN dnApache = new DN("dc=Apache,dc=Org");
157-
ServerEntry entryApache = service.newEntry(dnApache);
158-
entryApache.add("objectClass", "top", "domain", "extensibleObject");
159-
entryApache.add("dc", "Apache");
160-
service.getAdminSession().add(entryApache);
186+
try {
187+
if (!service.getAdminSession().exists(apachePartition.getSuffixDn())) {
188+
try{
189+
DN dnApache = new DN("dc=Apache,dc=Org");
190+
ServerEntry entryApache = service.newEntry(dnApache);
191+
entryApache.add("objectClass", "top", "domain", "extensibleObject");
192+
entryApache.add("dc", "Apache");
193+
service.getAdminSession().add(entryApache);
194+
} catch (Exception e) {
195+
System.out.println("Error creating new DN.");
196+
}
197+
}
198+
} catch (Exception e) {
199+
System.out.println("Error when checking if partition exists.");
161200
}
162201

163-
// We are all done !
164202
}
165203

166204
/**
@@ -184,7 +222,7 @@ private void initSchemaPartition() {
184222
SchemaLdifExtractor extractor = new DefaultSchemaLdifExtractor(wd);
185223
try {
186224
extractor.extractOrCopy( true );
187-
System.out.println("is Extracted: " + extractor.isExtracted());
225+
//System.out.println("is Extracted: " + extractor.isExtracted());
188226
} catch (Exception e) {
189227
}
190228

@@ -282,7 +320,8 @@ public void stopServer() throws Exception {
282320
* @throws Exception
283321
*/
284322
public static void main(String[] args) throws Exception {
285-
new LDAPServer();
323+
LDAPServer ldap = new LDAPServer();
324+
//ldap.stopServer();
286325
}
287326

288327
}

src/main/resources/log4j.properties

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Root logger option
2-
log4j.rootLogger=INFO, stdout
2+
log4j.rootLogger=ERROR, stdout
33

44
# Direct log messages to stdout
55
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
@@ -11,4 +11,15 @@ log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1
1111
log4j.logger.org.hibernate=INFO
1212

1313
# Log all JDBC parameters
14-
log4j.logger.org.hibernate.type=ALL
14+
log4j.logger.org.hibernate.type=ALL
15+
16+
# with these we'll not get inundated when switching to DEBUG
17+
log4j.logger.org.apache.directory.shared.ldap.name=ERROR
18+
log4j.logger.org.springframework=ERROR
19+
log4j.logger.org.apache.directory.shared.codec=ERROR
20+
log4j.logger.org.apache.directory.shared.asn1=ERROR
21+
log4j.logger.org.apache.directory.server.ldap.handlers.SearchHandler=ERROR
22+
log4j.logger.org.apache.directory.server.ldap.handlers.AddHandler=ERROR
23+
log4j.logger.org.apache.directory.server.ldap.handlers.DeleteHandler=ERROR
24+
log4j.logger.org.apache.directory.server.ldap.handlers.ModifyHandler=ERROR
25+
log4j.logger.org.apache.directory.server.ldap.handlers.ModifyDnHandler=ERROR

0 commit comments

Comments
 (0)