Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decode the logonHours attribute in Java

I am trying to decode the logonHours attribute from Active Directory so that it displays in my Java application properly. When I look in the attribute editor, it looks like this:

logonHours 03 00 00 00 F0 FF 1F F0 FF 1F F0 FF 1F F0 FF 1F F0 FF 1F 00 FF

This is precisely 21 bytes long, which is what I expect (each day is encoded in 3 bytes, for a total of 24 bits per day). This is meant to match the display under logonHours I have. However, my encoding is as follows (bit-strings for each day)

Sun:     00000000 (00) 00000000 (00) 00000000 (00)
Mon-Fri: 00001111 (0F) 11111111 (FF) 11111000 (F8)
Sat:     00000000 (00) 11111111 (FF) 11000000 (C0)

This makes me think that there is some encoding happening between the logonHours attribute and the display. I've looked at the Perl code and the VBScript - converting either of these in Java does a straight conversion, which does not match up with reality.

Does anyone has Java, C#.NET or other pseudo-code for decoding the logonHours attribute properly?

like image 543
Adrian Hall Avatar asked Dec 05 '25 18:12

Adrian Hall


1 Answers

I hate it when I figure these things out ten minutes later after I've thrown up my hands and asked for help. The key is in this page: Active Directory Permitted Logon Times with C# .Net 3.5 using System.DirectoryServices.AccountManagement - specifically, the ordering of the bits and the fact that the array starts with an off-by-1 issue. Here is the code for Java I came up with:

private String[] decodeLogonHours(Attribute attr)
{
    byte[] raw = attr.getValueByteArray();
    String[] days = new String[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    ArrayList<String> ret = new ArrayList<String>();

    for (int day = 0 ; day < days.length ; day++) {
        byte[] vBits;
        if (day == 6) {
            vBits = new byte[] { raw[19], raw[20], raw[0] };
        } else {
            vBits = new byte[] { raw[day*3+1], raw[day*3+2], raw[day*3+3] };
        }

        StringBuffer sb = new StringBuffer();
        sb.append(String.format("%s:", days[day]));
        for (int b = 0 ; b < 3 ; b++) {
            sb.append(" ");
            sb.append(decodeLogonBits(vBits[b]));
        }
        ret.add(sb.toString());
    }

    String[] r = new String[ret.size()];
    ret.toArray(r);
    return r;
}

private String decodeLogonBits(byte b) {
    StringBuffer sb = new StringBuffer();
    sb.append((b & 0x01) > 0 ? "1" : "0");
    sb.append((b & 0x02) > 0 ? "1" : "0");
    sb.append((b & 0x04) > 0 ? "1" : "0");
    sb.append((b & 0x08) > 0 ? "1" : "0");
    sb.append((b & 0x10) > 0 ? "1" : "0");
    sb.append((b & 0x20) > 0 ? "1" : "0");
    sb.append((b & 0x40) > 0 ? "1" : "0");
    sb.append((b & 0x80) > 0 ? "1" : "0");
    return sb.toString();
}
like image 183
Adrian Hall Avatar answered Dec 07 '25 08:12

Adrian Hall