Technical

HASHDIR

Both iPlanet Messaging Server and Sun Java Enterprise Messenger Server use an algorithm to store user mailboxes on disk. In order to locate a mailbox on disk, you’ll have to use the hashdir command. However the iMS and JES hashdir binary has all kinds of library dependencies and therfore not easy to use within self written scripts. But now there is an alternative! My co-worker Rob Wolfram has written a piece of code that reveals the algorithm used in hashdir.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/* hashdir.c - simple alternative for Sun iMS hashdir
               written by Rob Wolfram, <propdf@hamal.nl> */
 
#include <stdio.h>
#include <string.h>
 
int main(int argc, char *argv[])
{
    static char basis_hex[] = "0123456789abcdef";
    char result[8];
    char *name;
    unsigned long hashval = 0;
 
    if (!--argc)
    {
        printf("Usage: %s <uid>\n\n",argv[0]);
        return 1;
    }
    name=argv[1];
    while (*name)
        hashval = hashval*31 + *name++;
    result[0] = '/';
    result[1] = basis_hex[hashval & 0xf];
    hashval >>= 4;
    result[2] = basis_hex[hashval & 0x7];
    hashval >>= 3;
    result[3] = '/';
    result[4] = basis_hex[hashval & 0xf];
    hashval >>= 4;
    result[5] = basis_hex[hashval & 0x7];
    hashval >>= 3;
    result[6] = '/';
    result[7] = '\0';
    printf("%s\n",result);
    return 0;
}