1 | /** @file
|
---|
2 | *
|
---|
3 | * VirtualBox External Authentication Library:
|
---|
4 | * Windows Logon Authentication.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2016 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | /* If defined, debug messages will be written to the debugger. */
|
---|
20 | // #define AUTH_DEBUG
|
---|
21 |
|
---|
22 | #include <iprt/win/windows.h>
|
---|
23 |
|
---|
24 | #include <VBox/VBoxAuth.h>
|
---|
25 |
|
---|
26 | #ifdef AUTH_DEBUG
|
---|
27 | #include <stdio.h>
|
---|
28 |
|
---|
29 | static void dprintfw(const WCHAR *fmt, ...)
|
---|
30 | {
|
---|
31 | va_list va;
|
---|
32 | va_start(va, fmt);
|
---|
33 |
|
---|
34 | WCHAR buffer[1024];
|
---|
35 |
|
---|
36 | _vsnwprintf(buffer, sizeof (buffer), fmt, va);
|
---|
37 |
|
---|
38 | OutputDebugStringW(buffer);
|
---|
39 |
|
---|
40 | va_end(va);
|
---|
41 | }
|
---|
42 | #define DBGAUTH(a) dprintfw a
|
---|
43 | #else
|
---|
44 | #define DBGAUTH(a)
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | static WCHAR swszEmpty[] = { L"" };
|
---|
48 |
|
---|
49 | static void freeWideChar(WCHAR *pwszString)
|
---|
50 | {
|
---|
51 | if (pwszString && pwszString != &swszEmpty[0])
|
---|
52 | {
|
---|
53 | size_t cb = (wcslen(pwszString) + 1) * sizeof(WCHAR);
|
---|
54 | SecureZeroMemory(pwszString, cb);
|
---|
55 | free(pwszString);
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | static WCHAR *utf8ToWideChar(const char *pszString)
|
---|
60 | {
|
---|
61 | /*
|
---|
62 | * Shortcut for empty strings.
|
---|
63 | */
|
---|
64 | if (!pszString || *pszString == 0)
|
---|
65 | return &swszEmpty[0];
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * Return NULL on errors.
|
---|
69 | */
|
---|
70 | WCHAR *pwszString = NULL;
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * First calc result string length.
|
---|
74 | */
|
---|
75 | const DWORD dwFlags = MB_ERR_INVALID_CHARS;
|
---|
76 | int cwc = MultiByteToWideChar(CP_UTF8, dwFlags, pszString, -1, NULL, 0);
|
---|
77 | if (cwc > 0)
|
---|
78 | {
|
---|
79 | /*
|
---|
80 | * Alloc space for result buffer.
|
---|
81 | */
|
---|
82 | pwszString = (WCHAR *)malloc(cwc * sizeof(WCHAR));
|
---|
83 | if (pwszString)
|
---|
84 | {
|
---|
85 | /*
|
---|
86 | * Do the translation.
|
---|
87 | */
|
---|
88 | if (MultiByteToWideChar(CP_UTF8, dwFlags, pszString, -1, pwszString, cwc) <= 0)
|
---|
89 | {
|
---|
90 | /* translation error */
|
---|
91 | free(pwszString);
|
---|
92 | pwszString = NULL;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | return pwszString;
|
---|
98 | }
|
---|
99 |
|
---|
100 | extern "C"
|
---|
101 | #if defined(_MSC_VER)
|
---|
102 | __declspec(dllexport)
|
---|
103 | #endif
|
---|
104 | AuthResult AUTHCALL AuthEntry (const char *szCaller,
|
---|
105 | PAUTHUUID pUuid,
|
---|
106 | AuthGuestJudgement guestJudgement,
|
---|
107 | const char *szUser,
|
---|
108 | const char *szPassword,
|
---|
109 | const char *szDomain,
|
---|
110 | int fLogon,
|
---|
111 | unsigned clientId)
|
---|
112 | {
|
---|
113 | if (!fLogon)
|
---|
114 | {
|
---|
115 | /* Nothing to cleanup. The return code does not matter. */
|
---|
116 | return AuthResultAccessDenied;
|
---|
117 | }
|
---|
118 |
|
---|
119 | LPWSTR lpwszUsername = utf8ToWideChar(szUser);
|
---|
120 | LPWSTR lpwszDomain = utf8ToWideChar(szDomain);
|
---|
121 | LPWSTR lpwszPassword = utf8ToWideChar(szPassword);
|
---|
122 |
|
---|
123 | DBGAUTH((L"u[%ls], d[%ls], p[%ls]\n", lpwszUsername, lpwszDomain, lpwszPassword));
|
---|
124 |
|
---|
125 | AuthResult result = AuthResultAccessDenied;
|
---|
126 |
|
---|
127 | if (lpwszUsername && lpwszDomain && lpwszPassword)
|
---|
128 | {
|
---|
129 | /* LOGON32_LOGON_INTERACTIVE is intended for users who will be interactively using the computer,
|
---|
130 | * such as a user being logged on by a terminal server, remote shell, or similar process.
|
---|
131 | */
|
---|
132 | DWORD dwLogonType = LOGON32_LOGON_INTERACTIVE;
|
---|
133 | DWORD dwLogonProvider = LOGON32_PROVIDER_DEFAULT;
|
---|
134 |
|
---|
135 | HANDLE hToken;
|
---|
136 |
|
---|
137 | BOOL fSuccess = LogonUserW(lpwszUsername,
|
---|
138 | lpwszDomain,
|
---|
139 | lpwszPassword,
|
---|
140 | dwLogonType,
|
---|
141 | dwLogonProvider,
|
---|
142 | &hToken);
|
---|
143 |
|
---|
144 | if (fSuccess)
|
---|
145 | {
|
---|
146 | DBGAUTH((L"LogonUser success. hToken = %p\n", hToken));
|
---|
147 |
|
---|
148 | result = AuthResultAccessGranted;
|
---|
149 |
|
---|
150 | CloseHandle(hToken);
|
---|
151 | }
|
---|
152 | else
|
---|
153 | {
|
---|
154 | DBGAUTH((L"LogonUser failed %08X\n", GetLastError()));
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | freeWideChar(lpwszUsername);
|
---|
159 | freeWideChar(lpwszDomain);
|
---|
160 | freeWideChar(lpwszPassword);
|
---|
161 |
|
---|
162 | return result;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /* Verify the function prototype. */
|
---|
166 | static PAUTHENTRY3 gpfnAuthEntry = AuthEntry;
|
---|