Search in sources :

Example 1 with DS_DOMAIN_TRUSTS

use of com.sun.jna.platform.win32.DsGetDC.DS_DOMAIN_TRUSTS in project jna by java-native-access.

the class Netapi32Util method getDomainTrusts.

/**
     * Retrieve all domain trusts for a given server.
     * @param serverName
     *  Server name.
     * @return
     *  An array of domain trusts.
     */
public static DomainTrust[] getDomainTrusts(String serverName) {
    IntByReference domainTrustCount = new IntByReference();
    PointerByReference domainsPointerRef = new PointerByReference();
    int rc = Netapi32.INSTANCE.DsEnumerateDomainTrusts(serverName, DsGetDC.DS_DOMAIN_VALID_FLAGS, domainsPointerRef, domainTrustCount);
    if (W32Errors.NO_ERROR != rc) {
        throw new Win32Exception(rc);
    }
    try {
        DS_DOMAIN_TRUSTS domainTrustRefs = new DS_DOMAIN_TRUSTS(domainsPointerRef.getValue());
        DS_DOMAIN_TRUSTS[] domainTrusts = (DS_DOMAIN_TRUSTS[]) domainTrustRefs.toArray(new DS_DOMAIN_TRUSTS[domainTrustCount.getValue()]);
        ArrayList<DomainTrust> trusts = new ArrayList<DomainTrust>(domainTrustCount.getValue());
        for (DS_DOMAIN_TRUSTS domainTrust : domainTrusts) {
            DomainTrust t = new DomainTrust();
            if (domainTrust.DnsDomainName != null) {
                t.DnsDomainName = domainTrust.DnsDomainName.toString();
            }
            if (domainTrust.NetbiosDomainName != null) {
                t.NetbiosDomainName = domainTrust.NetbiosDomainName.toString();
            }
            t.DomainSid = domainTrust.DomainSid;
            if (domainTrust.DomainSid != null) {
                t.DomainSidString = Advapi32Util.convertSidToStringSid(domainTrust.DomainSid);
            }
            t.DomainGuid = domainTrust.DomainGuid;
            if (domainTrust.DomainGuid != null) {
                t.DomainGuidString = Ole32Util.getStringFromGUID(domainTrust.DomainGuid);
            }
            t.flags = domainTrust.Flags;
            trusts.add(t);
        }
        return trusts.toArray(new DomainTrust[0]);
    } finally {
        rc = Netapi32.INSTANCE.NetApiBufferFree(domainsPointerRef.getValue());
        if (W32Errors.NO_ERROR != rc) {
            throw new Win32Exception(rc);
        }
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) DS_DOMAIN_TRUSTS(com.sun.jna.platform.win32.DsGetDC.DS_DOMAIN_TRUSTS) ArrayList(java.util.ArrayList)

Example 2 with DS_DOMAIN_TRUSTS

use of com.sun.jna.platform.win32.DsGetDC.DS_DOMAIN_TRUSTS in project jna by java-native-access.

the class Netapi32Test method testDsEnumerateDomainTrusts.

public void testDsEnumerateDomainTrusts() {
    if (Netapi32Util.getJoinStatus() != LMJoin.NETSETUP_JOIN_STATUS.NetSetupDomainName)
        return;
    IntByReference domainTrustCount = new IntByReference();
    PointerByReference domainsPointerRef = new PointerByReference();
    assertEquals(W32Errors.NO_ERROR, Netapi32.INSTANCE.DsEnumerateDomainTrusts(null, DsGetDC.DS_DOMAIN_VALID_FLAGS, domainsPointerRef, domainTrustCount));
    assertTrue(domainTrustCount.getValue() >= 0);
    DS_DOMAIN_TRUSTS domainTrustRefs = new DS_DOMAIN_TRUSTS(domainsPointerRef.getValue());
    DS_DOMAIN_TRUSTS[] domainTrusts = (DS_DOMAIN_TRUSTS[]) domainTrustRefs.toArray(new DS_DOMAIN_TRUSTS[domainTrustCount.getValue()]);
    for (DS_DOMAIN_TRUSTS trust : domainTrusts) {
        assertTrue(trust.DnsDomainName.length() > 0);
        assertTrue(Advapi32.INSTANCE.IsValidSid(trust.DomainSid));
        assertTrue(Advapi32Util.convertSidToStringSid(trust.DomainSid).startsWith("S-"));
        assertTrue(Ole32Util.getStringFromGUID(trust.DomainGuid).startsWith("{"));
    }
    assertEquals(W32Errors.ERROR_SUCCESS, Netapi32.INSTANCE.NetApiBufferFree(domainTrustRefs.getPointer()));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) DS_DOMAIN_TRUSTS(com.sun.jna.platform.win32.DsGetDC.DS_DOMAIN_TRUSTS)

Aggregations

DS_DOMAIN_TRUSTS (com.sun.jna.platform.win32.DsGetDC.DS_DOMAIN_TRUSTS)2 IntByReference (com.sun.jna.ptr.IntByReference)2 PointerByReference (com.sun.jna.ptr.PointerByReference)2 ArrayList (java.util.ArrayList)1