Search in sources :

Example 16 with User

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

the class Netapi32Test method testNetUserGetGroups.

public void testNetUserGetGroups() {
    User[] users = Netapi32Util.getUsers();
    assertTrue(users.length >= 1);
    PointerByReference bufptr = new PointerByReference();
    IntByReference entriesread = new IntByReference();
    IntByReference totalentries = new IntByReference();
    assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetUserGetGroups(null, users[0].name, 0, bufptr, LMCons.MAX_PREFERRED_LENGTH, entriesread, totalentries));
    GROUP_USERS_INFO_0 lgroup = new GROUP_USERS_INFO_0(bufptr.getValue());
    GROUP_USERS_INFO_0[] lgroups = (GROUP_USERS_INFO_0[]) lgroup.toArray(entriesread.getValue());
    for (GROUP_USERS_INFO_0 localGroupInfo : lgroups) {
        assertTrue(localGroupInfo.grui0_name.length() > 0);
    }
    assertEquals(LMErr.NERR_Success, Netapi32.INSTANCE.NetApiBufferFree(bufptr.getValue()));
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) User(com.sun.jna.platform.win32.Netapi32Util.User) GROUP_USERS_INFO_0(com.sun.jna.platform.win32.LMAccess.GROUP_USERS_INFO_0) LOCALGROUP_USERS_INFO_0(com.sun.jna.platform.win32.LMAccess.LOCALGROUP_USERS_INFO_0) PointerByReference(com.sun.jna.ptr.PointerByReference)

Example 17 with User

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

the class Advapi32Test method testGetTokenUserInformation.

public void testGetTokenUserInformation() {
    HANDLEByReference phToken = new HANDLEByReference();
    try {
        HANDLE processHandle = Kernel32.INSTANCE.GetCurrentProcess();
        assertTrue(Advapi32.INSTANCE.OpenProcessToken(processHandle, WinNT.TOKEN_DUPLICATE | WinNT.TOKEN_QUERY, phToken));
        IntByReference tokenInformationLength = new IntByReference();
        assertFalse(Advapi32.INSTANCE.GetTokenInformation(phToken.getValue(), WinNT.TOKEN_INFORMATION_CLASS.TokenUser, null, 0, tokenInformationLength));
        assertEquals(W32Errors.ERROR_INSUFFICIENT_BUFFER, Kernel32.INSTANCE.GetLastError());
        WinNT.TOKEN_USER user = new WinNT.TOKEN_USER(tokenInformationLength.getValue());
        assertTrue(Advapi32.INSTANCE.GetTokenInformation(phToken.getValue(), WinNT.TOKEN_INFORMATION_CLASS.TokenUser, user, tokenInformationLength.getValue(), tokenInformationLength));
        assertTrue(tokenInformationLength.getValue() > 0);
        assertTrue(Advapi32.INSTANCE.IsValidSid(user.User.Sid));
        int sidLength = Advapi32.INSTANCE.GetLengthSid(user.User.Sid);
        assertTrue(sidLength > 0);
        assertTrue(sidLength < tokenInformationLength.getValue());
    // System.out.println(Advapi32Util.convertSidToStringSid(user.User.Sid));
    } finally {
        Kernel32Util.closeHandleRef(phToken);
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference) SC_HANDLE(com.sun.jna.platform.win32.Winsvc.SC_HANDLE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 18 with User

use of com.sun.jna.platform.win32.Netapi32Util.User in project gitblit by gitblit.

the class WindowsAuthProvider method authenticate.

@Override
public UserModel authenticate(String username, char[] password) {
    String defaultDomain = settings.getString(Keys.realm.windows.defaultDomain, null);
    if (StringUtils.isEmpty(defaultDomain)) {
        // ensure that default domain is null
        defaultDomain = null;
    }
    if (defaultDomain != null) {
        // sanitize username
        if (username.startsWith(defaultDomain + "\\")) {
            // strip default domain from domain\ username
            username = username.substring(defaultDomain.length() + 1);
        } else if (username.endsWith("@" + defaultDomain)) {
            // strip default domain from username@domain
            username = username.substring(0, username.lastIndexOf('@'));
        }
    }
    IWindowsIdentity identity = null;
    try {
        if (username.indexOf('@') > -1 || username.indexOf('\\') > -1) {
            // manually specified domain
            identity = waffle.logonUser(username, new String(password));
        } else {
            // no domain specified, use default domain
            identity = waffle.logonDomainUser(username, defaultDomain, new String(password));
        }
    } catch (Win32Exception e) {
        logger.error(e.getMessage());
        return null;
    }
    if (identity.isGuest() && !settings.getBoolean(Keys.realm.windows.allowGuests, false)) {
        logger.warn("Guest account access is disabled");
        identity.dispose();
        return null;
    }
    UserModel user = userManager.getUserModel(username);
    if (user == null) {
        // create user object for new authenticated user
        user = new UserModel(username.toLowerCase());
    }
    // create a user cookie
    setCookie(user);
    // update user attributes from Windows identity
    user.accountType = getAccountType();
    String fqn = identity.getFqn();
    if (fqn.indexOf('\\') > -1) {
        user.displayName = fqn.substring(fqn.lastIndexOf('\\') + 1);
    } else {
        user.displayName = fqn;
    }
    user.password = Constants.EXTERNAL_ACCOUNT;
    Set<String> groupNames = new TreeSet<String>();
    for (IWindowsAccount group : identity.getGroups()) {
        groupNames.add(group.getFqn());
    }
    if (settings.getBoolean(Keys.realm.windows.permitBuiltInAdministrators, true)) {
        if (groupNames.contains("BUILTIN\\Administrators")) {
            // local administrator
            user.canAdmin = true;
        }
    }
    // TODO consider mapping Windows groups to teams
    // push the changes to the backing user service
    updateUser(user);
    // cleanup resources
    identity.dispose();
    return user;
}
Also used : UserModel(com.gitblit.models.UserModel) TreeSet(java.util.TreeSet) IWindowsAccount(waffle.windows.auth.IWindowsAccount) IWindowsIdentity(waffle.windows.auth.IWindowsIdentity) Win32Exception(com.sun.jna.platform.win32.Win32Exception)

Example 19 with User

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

the class MSOfficeWordDemo method testMSWord.

public static void testMSWord() throws IOException {
    File demoDocument = null;
    ComIApplication msWord = null;
    Factory factory = new Factory();
    try {
        System.out.println("Files in temp dir: " + Helper.tempDir.getAbsolutePath());
        ComWord_Application msWordObject = factory.createObject(ComWord_Application.class);
        msWord = msWordObject.queryInterface(ComIApplication.class);
        System.out.println("MSWord version: " + msWord.getVersion());
        msWord.setVisible(true);
        demoDocument = Helper.createNotExistingFile("jnatest", ".doc");
        Helper.extractClasspathFileToReal("/com/sun/jna/platform/win32/COM/util/office/resources/jnatest.doc", demoDocument);
        msWord.getDocuments().Open(demoDocument.getAbsolutePath());
        Helper.sleep(5);
        msWord.getSelection().TypeText("Hello from JNA! \n\n");
        // wait 10sec. before closing
        Helper.sleep(10);
        // save in different formats
        // pdf format is only supported in MSWord 2007 and above
        msWord.getActiveDocument().SaveAs(new File(Helper.tempDir, "jnatestSaveAs.doc").getAbsolutePath(), WdSaveFormat.wdFormatDocument);
        msWord.getActiveDocument().SaveAs(new File(Helper.tempDir, "jnatestSaveAs.pdf").getAbsolutePath(), WdSaveFormat.wdFormatPDF);
        msWord.getActiveDocument().SaveAs(new File(Helper.tempDir, "jnatestSaveAs.rtf").getAbsolutePath(), WdSaveFormat.wdFormatRTF);
        msWord.getActiveDocument().SaveAs(new File(Helper.tempDir, "jnatestSaveAs.html").getAbsolutePath(), WdSaveFormat.wdFormatHTML);
        // close and don't save the changes
        msWord.getActiveDocument().Close(false);
        // Create a new document
        msWord.getDocuments().Add();
        // msWord.openDocument(currentWorkingDir + "jnatest.doc", true);
        msWord.getSelection().TypeText("Hello from JNA! \n Please notice that JNA can control " + "MS Word via the new COM interface! \nHere we are " + "creating a new word document and we save it " + "to the 'TEMP' directory!");
        // save with no user prompt
        msWord.getActiveDocument().SaveAs(new File(Helper.tempDir, "jnatestNewDoc1.docx").getAbsolutePath(), WdSaveFormat.wdFormatDocumentDefault);
        msWord.getActiveDocument().SaveAs(new File(Helper.tempDir, "jnatestNewDoc2.docx").getAbsolutePath(), WdSaveFormat.wdFormatDocumentDefault);
        msWord.getActiveDocument().SaveAs(new File(Helper.tempDir, "jnatestNewDoc3.docx").getAbsolutePath(), WdSaveFormat.wdFormatDocumentDefault);
        // close and don't save the changes
        msWord.getActiveDocument().Close(false);
        // open 3 documents
        msWord.getDocuments().Open(new File(Helper.tempDir, "jnatestNewDoc1.docx").getAbsolutePath());
        msWord.getSelection().TypeText("Hello some changes from JNA!\n");
        msWord.getDocuments().Open(new File(Helper.tempDir, "jnatestNewDoc2.docx").getAbsolutePath());
        msWord.getSelection().TypeText("Hello some changes from JNA!\n");
        msWord.getDocuments().Open(new File(Helper.tempDir, "jnatestNewDoc3.docx").getAbsolutePath());
        msWord.getSelection().TypeText("Hello some changes from JNA!\n");
        // save the document and prompt the user
        msWord.getDocuments().Save(false, WdOriginalFormat.wdPromptUser);
    } finally {
        // Make sure the word instance is shut down
        if (msWord != null) {
            msWord.Quit();
        }
        // Release all objects acquired by the factory
        factory.disposeAll();
        if (demoDocument != null && demoDocument.exists()) {
            demoDocument.delete();
        }
    }
}
Also used : ComWord_Application(com.sun.jna.platform.win32.COM.util.office.word.ComWord_Application) Factory(com.sun.jna.platform.win32.COM.util.Factory) File(java.io.File) ComIApplication(com.sun.jna.platform.win32.COM.util.office.word.ComIApplication)

Example 20 with User

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

the class AlphaMaskDemo method updateW32.

private void updateW32(boolean a, boolean i) {
    User32 user = User32.INSTANCE;
    GDI32 gdi = GDI32.INSTANCE;
    HWND hWnd = null;
    if (!alphaWindow.isDisplayable()) {
        alphaWindow.pack();
        hWnd = getHwnd(alphaWindow);
        int flags = user.GetWindowLong(hWnd, WinUser.GWL_EXSTYLE);
        flags |= WinUser.WS_EX_LAYERED;
        user.SetWindowLong(hWnd, WinUser.GWL_EXSTYLE, flags);
        Window parent = alphaWindow.getOwner();
        Point where = parent.getLocationOnScreen();
        where.translate(parent.getWidth(), 0);
        alphaWindow.setLocation(where);
    } else {
        hWnd = getHwnd(alphaWindow);
    }
    if (i) {
        int w = image.getWidth(null);
        int h = image.getHeight(null);
        HDC screenDC = user.GetDC(null);
        HDC memDC = gdi.CreateCompatibleDC(screenDC);
        HBITMAP hBitmap = null;
        HANDLE oldBitmap = null;
        try {
            BufferedImage buf = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
            Graphics g = buf.getGraphics();
            g.drawImage(image, 0, 0, w, h, null);
            BITMAPINFO bmi = new BITMAPINFO();
            bmi.bmiHeader.biWidth = w;
            bmi.bmiHeader.biHeight = h;
            bmi.bmiHeader.biPlanes = 1;
            bmi.bmiHeader.biBitCount = 32;
            bmi.bmiHeader.biCompression = WinGDI.BI_RGB;
            bmi.bmiHeader.biSizeImage = w * h * 4;
            PointerByReference ppbits = new PointerByReference();
            hBitmap = gdi.CreateDIBSection(memDC, bmi, WinGDI.DIB_RGB_COLORS, ppbits, null, 0);
            oldBitmap = gdi.SelectObject(memDC, hBitmap);
            Pointer pbits = ppbits.getValue();
            Raster raster = buf.getData();
            int[] pixel = new int[4];
            int[] bits = new int[w * h];
            for (int y = 0; y < h; y++) {
                for (int x = 0; x < w; x++) {
                    raster.getPixel(x, h - y - 1, pixel);
                    int alpha = (pixel[3] & 0xFF) << 24;
                    int red = (pixel[2] & 0xFF);
                    int green = (pixel[1] & 0xFF) << 8;
                    int blue = (pixel[0] & 0xFF) << 16;
                    bits[x + y * w] = alpha | red | green | blue;
                }
            }
            pbits.write(0, bits, 0, bits.length);
            SIZE size = new SIZE();
            size.cx = w;
            size.cy = h;
            POINT loc = new POINT();
            loc.x = alphaWindow.getX();
            loc.y = alphaWindow.getY();
            POINT srcLoc = new POINT();
            BLENDFUNCTION blend = new BLENDFUNCTION();
            blend.SourceConstantAlpha = (byte) (alpha * 255);
            blend.AlphaFormat = WinUser.AC_SRC_ALPHA;
            user.UpdateLayeredWindow(hWnd, screenDC, loc, size, memDC, srcLoc, 0, blend, WinUser.ULW_ALPHA);
        } finally {
            user.ReleaseDC(null, screenDC);
            if (hBitmap != null) {
                gdi.SelectObject(memDC, oldBitmap);
                gdi.DeleteObject(hBitmap);
            }
            gdi.DeleteDC(memDC);
        }
    } else if (a) {
        BLENDFUNCTION blend = new BLENDFUNCTION();
        blend.SourceConstantAlpha = (byte) (alpha * 255);
        blend.AlphaFormat = WinUser.AC_SRC_ALPHA;
        user.UpdateLayeredWindow(hWnd, null, null, null, null, null, 0, blend, WinUser.ULW_ALPHA);
    }
    if (!alphaWindow.isVisible()) {
        alphaWindow.setVisible(true);
    }
}
Also used : Window(java.awt.Window) JWindow(javax.swing.JWindow) GDI32(com.sun.jna.platform.win32.GDI32) BLENDFUNCTION(com.sun.jna.platform.win32.WinUser.BLENDFUNCTION) HDC(com.sun.jna.platform.win32.WinDef.HDC) HWND(com.sun.jna.platform.win32.WinDef.HWND) Raster(java.awt.image.Raster) SIZE(com.sun.jna.platform.win32.WinUser.SIZE) Pointer(com.sun.jna.Pointer) Point(java.awt.Point) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) Graphics(java.awt.Graphics) BITMAPINFO(com.sun.jna.platform.win32.WinGDI.BITMAPINFO) PointerByReference(com.sun.jna.ptr.PointerByReference) User32(com.sun.jna.platform.win32.User32) HBITMAP(com.sun.jna.platform.win32.WinDef.HBITMAP) POINT(com.sun.jna.platform.win32.WinDef.POINT) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Aggregations

HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)7 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)6 IntByReference (com.sun.jna.ptr.IntByReference)6 PointerByReference (com.sun.jna.ptr.PointerByReference)6 USER_INFO_1 (com.sun.jna.platform.win32.LMAccess.USER_INFO_1)5 LOCALGROUP_USERS_INFO_0 (com.sun.jna.platform.win32.LMAccess.LOCALGROUP_USERS_INFO_0)3 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 WString (com.sun.jna.WString)2 Account (com.sun.jna.platform.win32.Advapi32Util.Account)2 CLSID (com.sun.jna.platform.win32.Guid.CLSID)2 GROUP_USERS_INFO_0 (com.sun.jna.platform.win32.LMAccess.GROUP_USERS_INFO_0)2 HWND (com.sun.jna.platform.win32.WinDef.HWND)2 LCID (com.sun.jna.platform.win32.WinDef.LCID)2 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)2 UserModel (com.gitblit.models.UserModel)1 Pointer (com.sun.jna.Pointer)1 COMException (com.sun.jna.platform.win32.COM.COMException)1 Factory (com.sun.jna.platform.win32.COM.util.Factory)1