Archive for January, 2006

Installing LDAP on Debian

Installing LDAP is not that simple. Some basic LDAP knowledge is needed, but for setting up quickly do;

apt-get install slapd ldap-utils migrationtools

Answer all questions; make sure you enter for the first to screens (DN and org) the same name, for instance domain.com. You don’t have to do this, but if you don’t know anything about LDAP this is advisable, because you’ll get errors like;

ldap_bind: Invalid credentials

Now you can add your /etc/passwd users by migrating them with:

cd /usr/share/migrationtools
./migrate_passwd.pl /etc/passwd /tmp/passwd.ldif

When importing these like:

ldapadd -x -v -D cn=admin,dc=domain,dc=com -w YOURPASSWORD < /tmp/passwd.ldif You’ll probably get some error like: parent does not exist Because your passwd.ldif contains: ou: Groups If you want to know why, you should look at an organizational schema of LDAP, but if you don’t care, just make an ldif file like: dn: ou=People,dc=domain,dc=com
ou: People
objectClass: organizationalUnit

And add it;

ldapadd -x -v -D cn=admin,dc=domain,dc=com -w YOURPASSWORD < /tmp/ou.ldif

Now add the passwd.diff

ldapadd -x -v -D cn=admin,dc=domain,dc=com -w YOURPASSWORD < /tmp/passwd.ldif and check if all went ok; /usr/bin/ldapsearch -x -p 389 -h localhost -w YOURPASSWD -D ‘cn=admin,dc=componence,dc=com’ objectClass=* It should show all you added and set.

Waiting for all threads to end in Java

Looking in Google for some easy out-of-the-box way for waiting for all threads to end and then continue the main() (or something) method, I found that no simple way was indicated anywhere. Use thread.join(). Yeah sure, only problem is that I have multiple threads and I want all of them to run and time the running time.

For example: I have 4 threads:

Thread t1,t2,t3,t4;

which I run from a main() method in a Java class. The main method looks like this:

public void main(String[] args) {
… some stuff before

// first run
t1.start();
t2.start();
t3. start();
t4.start();

// second run
t1.start();
t2.start();
t3. start();
t4.start();
}

Now I want to not start the second run before the first is over. A way to do this, is by running:

public void main(String[] args) {
… some stuff before

// first run
t1.start();
t2.start();
t3. start();
t4.start();

// wait till all of these are done
t1.join();
t2.join();
t3. join();
t4.join();
… etc

For this trivial case, this is ok, but in the real program I am writing, a lot of threads are started, for instance from classes.methods all over the place, making keeping references to them for joining quite a challenge.

Anyway; Google did not indicate to me a straight, simple and generic way of doing it, so I did the following:

class ThreadCheck extends Thread {
List l = new ArrayList();

public void queueThread(Thread t) {
l.add(t);
}

public void run() {
for (int i=0;i<l.size();i++) {
try {
l.get(i).join();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Now you can add your Threads to the object;

tc=ThreadCheck();
t1.start();
tc.queueThread(t1);t2.start();
tc.queueThread(t2);
etc

I usually pass tc to the the Thread when creating it, so it can queue itself after it is started; this makes it even easier, because you don’t need any references to the threads after creation.Then you can start the tc;

tc.start();

and make sure all ends only when all threads are done:

tc.join();

To time the execution:

long st= System.currentTimeMillis();itc.start(); tc.join();
long et= System.currentTimeMillis()-st;
System.out.println(“Stuff ran for “+et+” milliseconds”);

The above method works very well, but if anyone knows a better method, please mail me!

Catching Post Redirects with HttpClient

For some very good reasons, RFC 2616 and HttpClient do not allow automatic POST redirects. A good reason, but irritating it is nontheless.
I had to get around it today for a bunch of cases which are clear and very common; the case where a POST continues into a GET. For instance; you entered a login/password and are redirected to another page when it is right/wrong without the need for the POST info to be posted to that page also.

The following code has been written for a simple proxy server, so everything is passed to the response;

First of all, you need to know if there is a redirect going on;

method is a PostMethod, request is the HttpServletRequest, response is the HttpServletResponse;

if (request.getMethod().equals(“POST”) && method.getResponseHeader(“location”)!=null) {

Then you don’t need the current method anymore, because you are redirecting;

method.releaseConnection();

Get the location to redirect to;

String location = method.getResponseHeader(“location”).getValue();

And make a new GetMethod:

method = new GetMethod(dst+’/’+location);

dst is the destination domain which you know ofcourse, because you cannot use HttpClient without it.

Run the method;

statusCode = client.executeMethod(method);

Make sure the response knows that this is another URL: you can experiment with this to show any URL in the browser of the client:

response.setStatus(HttpStatus.SC_MOVED_TEMPORARILY);
response.setHeader(“Location”, location);

This works fine for the cases I tried like logging in and filling forms.