Search in sources :

Example 1 with DomainTrust

use of com.sun.jna.platform.win32.Netapi32Util.DomainTrust in project jna by java-native-access.

the class Netapi32UtilTest method main.

public static void main(String[] args) {
    junit.textui.TestRunner.run(Netapi32UtilTest.class);
    System.out.println("Domain: " + Netapi32Util.getDomainName("localhost"));
    // server local groups
    Netapi32Util.LocalGroup[] localGroups = Netapi32Util.getLocalGroups();
    System.out.println("Local groups: " + localGroups.length);
    for (Netapi32Util.LocalGroup localGroup : localGroups) {
        System.out.println(" " + localGroup.name + " (" + localGroup.comment + ")");
    }
    // global groups
    Netapi32Util.Group[] groups = Netapi32Util.getGlobalGroups();
    System.out.println("Global groups: " + groups.length);
    for (Netapi32Util.Group group : groups) {
        System.out.println(" " + group.name);
    }
    // server users
    Netapi32Util.User[] users = Netapi32Util.getUsers();
    System.out.println("Users: " + users.length);
    for (Netapi32Util.User user : users) {
        System.out.println(" " + user.name);
    }
    // user local groups
    Netapi32Util.Group[] userLocalGroups = Netapi32Util.getCurrentUserLocalGroups();
    System.out.println("Local user groups: " + userLocalGroups.length);
    for (Netapi32Util.Group localGroup : userLocalGroups) {
        System.out.println(" " + localGroup.name);
    }
    // domain controller
    if (Netapi32Util.getJoinStatus() == LMJoin.NETSETUP_JOIN_STATUS.NetSetupDomainName) {
        System.out.println("Pdc: " + Netapi32Util.getDCName());
        DomainController dc = Netapi32Util.getDC();
        System.out.println("Domain controller:");
        System.out.println("    name: " + dc.name);
        System.out.println(" address: " + dc.address);
        System.out.println("  domain: " + dc.domainName);
        System.out.println("    site: " + dc.clientSiteName);
        System.out.println("  forest: " + dc.dnsForestName);
        System.out.println("    guid: " + Ole32Util.getStringFromGUID(dc.domainGuid));
    }
    // domain trusts
    if (Netapi32Util.getJoinStatus() == LMJoin.NETSETUP_JOIN_STATUS.NetSetupDomainName) {
        DomainTrust[] trusts = Netapi32Util.getDomainTrusts();
        System.out.println("Domain trusts: (" + trusts.length + ")");
        for (DomainTrust trust : trusts) {
            System.out.println(" " + trust.NetbiosDomainName + ": " + trust.DnsDomainName + " (" + trust.DomainSidString + ")");
        }
    }
}
Also used : DomainController(com.sun.jna.platform.win32.Netapi32Util.DomainController) DomainTrust(com.sun.jna.platform.win32.Netapi32Util.DomainTrust)

Example 2 with DomainTrust

use of com.sun.jna.platform.win32.Netapi32Util.DomainTrust 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 3 with DomainTrust

use of com.sun.jna.platform.win32.Netapi32Util.DomainTrust in project jna by java-native-access.

the class Netapi32UtilTest method testGetDomainTrusts.

public void testGetDomainTrusts() {
    if (Netapi32Util.getJoinStatus() != LMJoin.NETSETUP_JOIN_STATUS.NetSetupDomainName)
        return;
    DomainTrust[] trusts = Netapi32Util.getDomainTrusts();
    assertTrue(trusts.length >= 0);
    for (DomainTrust trust : trusts) {
        assertTrue(trust.NetbiosDomainName.length() > 0);
        assertTrue(trust.DnsDomainName.length() > 0);
        assertTrue(Advapi32.INSTANCE.IsValidSid(trust.DomainSid));
        assertTrue(trust.isInbound() || trust.isOutbound() || trust.isPrimary());
    }
}
Also used : DomainTrust(com.sun.jna.platform.win32.Netapi32Util.DomainTrust)

Aggregations

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