[jcifs] Problems with implementing LsarLookupSids()

Ralf Hartmann Ralf.Hartmann at xsystem.de
Fri Dec 10 10:48:25 GMT 2004


Hello Mr. Allen,

i want to use jarapac 0.3.7 to resolve SID's to user names. I have changed the 
IDL to support LsarLookupSids() (please refer the attachment). The IDL has been 
compiled with MIDL 0.5.1. The request looks fine in the network trace. Also the 
response from the W2K system looks OK. But the decoding of the network packet 
leads to a crash (please see below). I have figured out, that the unicode_string 
name in lsa_TrustInformation  doesn't get initalized. I have changed this in the 
Java source.  But this leads to another crash in decoding the response.

Can you help me please? Maybe you have implemented LsarLookupSids in the IDL but 
not distributed it yet. Or you have a hint what is wrong with my IDL.

Maybe the SID.java i have used for the testing is for interest of you. So i send 
it as an attachment.

Next i will try to implement NTQuerySecurityDesc as an addon to JCIFS. I need 
both (LsarLookupSids and NTQuerySecurityDesc) to display the file owner.

JCIFS works very well. It is a great software.

Best Regards
Ralf Hartmann

------------------------------------------------------------------------------
Call:
        public void lookupSID() throws Exception {
                 rpc.policy_handle handle = lsarOpenPolicy();
                 lsa_SidArray sids = new lsa_SidArray();
                 sids.num_sids = 1;
                 sids.sids = new lsa_SidPtr[sids.num_sids];
                 sids.sids[0] = new lsa_SidPtr();
                 sids.sids[0].sid = SID.toSID(
                         "S-1-5-21-1319786636-1892384142-370684871-1003");

                 lsa_RefDomainList domains = new lsa_RefDomainList();
                 lsa_TransNameArray names = new lsa_TransNameArray();

                 LsarLookupSids req = new LsarLookupSids(
                                 handle,
                                 sids,
                                 domains,
                                 names,
                                 (short) 1,
                                 1);
                 call(0, req);
                 if( req.retval != 0 ) {
                         throw new Exception( "0x" + Hexdump.toHexString( 
req.retval, 8 ));
                 }

                 lsarClose(handle);
         }
------------------------------------------------------------------------------
Result:
00000: 04 28 92 00 02 00 00 00 00 00 00 00 02 00 00 00  |.(..............|
00010: 5C 00 00 00 18 00 00 00 00 00 00 00 00 00 00 00  |\...............|
00020: 00 00 00 00 00 00 00 00 59 58 81 01 0C 00 00 00  |........YX......|
00030: 02 00 01 00 00 00 00 02                          |........        |

00000: 00 00 00 00 2A 87 15 84 34 2B 8C 46 85 36 56 55  |....*...4+.F.6VU|
00010: C3 E3 48 EA 00 00 00 00                          |??H?....        |

0: -2078963926
00000: 00 00 00 00 2A 87 15 84 34 2B 8C 46 85 36 56 55  |....*...4+.F.6VU|
00010: C3 E3 48 EA 01 00 00 00 3B 85 AB 00 01 00 00 00  |??H?....;.?.....|
00020: 68 23 B8 00 05 00 00 00 01 05 00 00 00 00 00 05  |h#?.............|
00030: 15 00 00 00 8C 58 AA 4E 8E 7D CB 70 C7 33 18 16  |.....X?N.}?p?3..|
00040: EB 03 00 00 00 00 00 00 00 00 00 00 01 00 00 00  |?...............|
00050: 01 00 00 00                                      |....            |

00000: 50 FA 0B 00 01 00 00 00 B0 DD 08 00 20 00 00 00  |P?......??.. ...|
00010: 01 00 00 00 0A 00 0C 00 E8 F5 0B 00 E0 E2 0B 00  |........??..??..|
00020: 06 00 00 00 00 00 00 00 05 00 00 00 4E 00 54 00  |............N.T.|
00030: 50 00 43 00 36 00 00 00 04 00 00 00 01 04 00 00  |P.C.6...........|
00040: 00 00 00 05 15 00 00 00 8C 58 AA 4E 8E 7D CB 70  |.........X?N.}?p|
00050: C7 33 18 16 01 00 00 00 78 FD 0B 00 01 00 00 00  |?3......x?......|
00060: 01 00 0E 00 08 00 0A 00 60 B2 09 00 00 00 00 00  |........`?......|
00070: 05 00 00 00 00 00 00 00 04 00 00 00 72 00 61 00  |............r.a.|
00080: 6C 00 66 00 01 00 00 00 00 00 00 00              |l.f.........    |

Exception in thread "main" java.lang.NullPointerException
         at lsarpc$lsa_TrustInformation.decode(lsarpc.java:273)
         at lsarpc$lsa_RefDomainList.decode(lsarpc.java:348)
         at lsarpc$LsarLookupSids.decode(lsarpc.java:541)
         at rpc.ConnectionOrientedEndpoint.call(ConnectionOrientedEndpoint.java:94)
         at rpc.Stub.call(Stub.java:113)
         at TestLsarpc.lookupSID(TestLsarpc.java:91)
         at TestLsarpc.main(TestLsarpc.java:114)

------------------------------------------------------------------------------
-- 
Ralf Hartmann                           Tel.:   +49 89 746603 0
X-System GmbH                           Fax:    +49 89 746603 99
Bavariastr. 7a                          E-Mail: Ralf.Hartmann at Xsystem.De
80336 Muenchen
-------------- next part --------------
[
	uuid(12345778-1234-abcd-ef00-0123456789ab),
	version(0.0)
]
interface lsarpc
{
	import "rpc.idl";

	typedef struct {
		uint32_t length;
		uint16_t impersonation_level;
		uint8_t context_mode;
		uint8_t effective_only;
	} LsaQosInfo;

	typedef struct {
		uint32_t length;
		uint8_t *root_directory;
		unicode_string *object_name;
		uint32_t attributes;
		uint32_t security_descriptor;
		LsaQosInfo *security_quality_of_service;
	} LsaObjectAttributes;

	typedef struct {
		unicode_string name;
		sid_t *sid;
	} LsaDomainInfo;

	enum {
		POLICY_INFO_AUDIT_EVENTS = 2,
		POLICY_INFO_PRIMARY_DOMAIN = 3,
		POLICY_INFO_ACCOUNT_DOMAIN = 5,
		POLICY_INFO_SERVER_ROLE = 6,
		POLICY_INFO_MODIFICATION = 9,
		POLICY_INFO_DNS_DOMAIN = 12
	};

	typedef [switch_type(short)] union {
		[case(POLICY_INFO_ACCOUNT_DOMAIN)] LsaDomainInfo account_domain;
	} LsaPolicyInfo;

        typedef struct {
                sid_t *sid;
        } lsa_SidPtr;

        typedef [public] struct {
                [range(0,1000)] uint32_t num_sids;
                [size_is(num_sids)] lsa_SidPtr *sids;
        } lsa_SidArray;

        typedef struct {
                unicode_string name;
                sid_t  *sid;
        } lsa_TrustInformation;

        typedef struct {
                [range(0,1000)] uint32_t count;
                [size_is(count)] lsa_TrustInformation *domains;
                uint32_t max_count;
        } lsa_RefDomainList;

        typedef struct {
                uint16_t sid_type;
                unicode_string name;
                uint32_t sid_index;
        } lsa_TranslatedName;

        typedef struct {
                [range(0,1000)] uint32_t count;
                [size_is(count)] lsa_TranslatedName *names;
        } lsa_TransNameArray;

	[op(0x00)]
	int LsarClose([in,out] policy_handle *handle);

	[op(0x07)]
	int LsarQueryInformationPolicy([in] policy_handle *handle,
			[in] uint16_t level,
			[out,switch_is(level),unique] LsaPolicyInfo *info);

	[op(0x0f)]
	int LsarLookupSids([in] policy_handle *handle,
			[in] lsa_SidArray *sids,
			[out] lsa_RefDomainList *domains,
			[in,out] lsa_TransNameArray *names,
			[in] uint16_t level,
			[in,out] uint32_t *count
                );

	[op(0x2c)]
	int LsarOpenPolicy([in,string,unique] wchar_t *system_name,
			[in] LsaObjectAttributes *object_attributes,
			[in] uint32_t desired_access,
			[out] policy_handle *policy_handle);
}
-------------- next part --------------
import java.util.*;

public class SID extends rpc.sid_t {

	public static String toTextual(rpc.sid_t sid) {
	    String ret = "S-" + sid.revision + "-";
	    if (sid.identifier_authority[0] != 0 ||
		sid.identifier_authority[1] != 0) {
		ret += "0x";
		for (int i = 0; i < 6; i++)
		    ret += Integer.toHexString(sid.identifier_authority[i]);
	    }
	    else {
		int shift = 0;
		long id = 0;
		for (int i = 5; i > 1; i--) {
		    id += ((long) sid.identifier_authority[i]) << shift;
		    shift += 8;
		}
		ret += id;
	    }

	    for (int i = 0; i < sid.sub_authority_count ; i++)
		ret += "-" + sid.sub_authority[i];

	    return ret;
	}
	public static rpc.sid_t toSID(String textual) throws Exception {

	    rpc.sid_t sid = new rpc.sid_t();

	    try {
		StringTokenizer st = new StringTokenizer(textual, "-");
		if (st.countTokens() < 3 || !st.nextToken().equals("S"))
		    // need S-N-M
		    throw new Exception("bad format");

		sid.revision = Byte.parseByte(st.nextToken());
		String tmp = st.nextToken();
		long id = 0;
		if (tmp.startsWith("0x"))
		    id = Long.parseLong(tmp.substring(2), 16);
		else
		    id = Long.parseLong(tmp);

		sid.identifier_authority = new byte[6];
		for (int i = 5; id > 0;  i--) {
		    sid.identifier_authority[i] = (byte) (id % 256);
		    id >>= 8;
                }

		sid.sub_authority_count = (byte) st.countTokens();
		if (sid.sub_authority_count > 0) {
		    sid.sub_authority = new int[sid.sub_authority_count];
		    for (int i = 0; i < sid.sub_authority_count; i++)
			sid.sub_authority[i] = Integer.parseInt(st.nextToken());
		}
	    }
	    catch (Exception e) {
		System.out.println("toSID: Exeception: " + e.toString());
		return null;
	    }
	    return sid;
	}
}

/*

BOOL GetTextualSid(
    PSID pSid,            // binary SID
    LPTSTR TextualSid,    // buffer for Textual representation of SID
    LPDWORD lpdwBufferLen // required/provided TextualSid buffersize
    )
{
    PSID_IDENTIFIER_AUTHORITY psia;
    DWORD dwSubAuthorities;
    DWORD dwSidRev=SID_REVISION;
    DWORD dwCounter;
    DWORD dwSidSize;

    // Validate the binary SID.

    if(!IsValidSid(pSid)) return FALSE;

    // Get the identifier authority value from the SID.

    psia = GetSidIdentifierAuthority(pSid);

    // Get the number of subauthorities in the SID.

    dwSubAuthorities = *GetSidSubAuthorityCount(pSid);

    // Compute the buffer length.
    // S-SID_REVISION- + IdentifierAuthority- + subauthorities- + NULL

    dwSidSize=(15 + 12 + (12 * dwSubAuthorities) + 1) * sizeof(TCHAR);

    // Check input buffer length.
    // If too small, indicate the proper size and set the last error.

    if (*lpdwBufferLen < dwSidSize)
    {
        *lpdwBufferLen = dwSidSize;
        SetLastError(ERROR_INSUFFICIENT_BUFFER);
        return FALSE;
    }

    // Add 'S' prefix and revision number to the string.

    dwSidSize=wsprintf(TextualSid, TEXT("S-%lu-"), dwSidRev );

    // Add a SID identifier authority to the string.

    if ( (psia->Value[0] != 0) || (psia->Value[1] != 0) )
    {
        dwSidSize+=wsprintf(TextualSid + lstrlen(TextualSid),
                    TEXT("0x%02hx%02hx%02hx%02hx%02hx%02hx"),
                    (USHORT)psia->Value[0],
                    (USHORT)psia->Value[1],
                    (USHORT)psia->Value[2],
                    (USHORT)psia->Value[3],
                    (USHORT)psia->Value[4],
                    (USHORT)psia->Value[5]);
    }
    else
    {
        dwSidSize+=wsprintf(TextualSid + lstrlen(TextualSid),
                    TEXT("%lu"),
                    (ULONG)(psia->Value[5]      )   +
                    (ULONG)(psia->Value[4] <<  8)   +
                    (ULONG)(psia->Value[3] << 16)   +
                    (ULONG)(psia->Value[2] << 24)   );
    }

    // Add SID subauthorities to the string.
    //
    for (dwCounter=0 ; dwCounter < dwSubAuthorities ; dwCounter++)
    {
        dwSidSize+=wsprintf(TextualSid + dwSidSize, TEXT("-%lu"),
                    *GetSidSubAuthority(pSid, dwCounter) );
    }

    return TRUE;
}
*/


More information about the jcifs mailing list