d071fd2e3168594476aeb9b6438c9b822b6ce799
[reactos.git] / reactos / dll / win32 / msgina / tui.c
1 /*
2 * PROJECT: ReactOS msgina.dll
3 * FILE: dll/win32/msgina/tui.c
4 * PURPOSE: ReactOS Logon GINA DLL
5 * PROGRAMMER: Hervé Poussineau (hpoussin@reactos.org)
6 */
7
8 #include "msgina.h"
9
10 static BOOL
11 TUIInitialize(
12 IN OUT PGINA_CONTEXT pgContext)
13 {
14 TRACE("TUIInitialize(%p)\n", pgContext);
15
16 return AllocConsole();
17 }
18
19 static BOOL
20 TUIDisplayStatusMessage(
21 IN PGINA_CONTEXT pgContext,
22 IN HDESK hDesktop,
23 IN DWORD dwOptions,
24 IN PWSTR pTitle,
25 IN PWSTR pMessage)
26 {
27 static LPCWSTR newLine = L"\n";
28 DWORD result;
29
30 TRACE("TUIDisplayStatusMessage(%ws)\n", pMessage);
31
32 UNREFERENCED_PARAMETER(pgContext);
33 UNREFERENCED_PARAMETER(hDesktop);
34 UNREFERENCED_PARAMETER(dwOptions);
35 UNREFERENCED_PARAMETER(pTitle);
36
37 return
38 WriteConsoleW(
39 GetStdHandle(STD_OUTPUT_HANDLE),
40 pMessage,
41 wcslen(pMessage),
42 &result,
43 NULL) &&
44 WriteConsoleW(
45 GetStdHandle(STD_OUTPUT_HANDLE),
46 newLine,
47 wcslen(newLine),
48 &result,
49 NULL);
50 }
51
52 static BOOL
53 TUIRemoveStatusMessage(
54 IN PGINA_CONTEXT pgContext)
55 {
56 UNREFERENCED_PARAMETER(pgContext);
57
58 /* Nothing to do */
59 return TRUE;
60 }
61
62 static BOOL
63 DisplayResourceText(
64 IN UINT uIdResourceText,
65 IN BOOL AddNewLine)
66 {
67 WCHAR Prompt[256];
68 static LPCWSTR newLine = L"\n";
69 DWORD count;
70
71 if (0 == LoadStringW(hDllInstance, uIdResourceText, Prompt, 256))
72 return FALSE;
73 if (!WriteConsoleW(
74 GetStdHandle(STD_OUTPUT_HANDLE),
75 Prompt, wcslen(Prompt),
76 &count, NULL))
77 {
78 return FALSE;
79 }
80 if (AddNewLine)
81 {
82 if (!WriteConsoleW(
83 GetStdHandle(STD_OUTPUT_HANDLE),
84 newLine, wcslen(newLine),
85 &count, NULL))
86 {
87 return FALSE;
88 }
89 }
90 return TRUE;
91 }
92
93 static VOID
94 TUIDisplaySASNotice(
95 IN OUT PGINA_CONTEXT pgContext)
96 {
97 TRACE("TUIDisplaySASNotice()\n");
98
99 UNREFERENCED_PARAMETER(pgContext);
100
101 DisplayResourceText(IDS_LOGGEDOUTSAS, TRUE);
102 DisplayResourceText(IDS_PRESSCTRLALTDELETE, TRUE);
103 }
104
105 static INT
106 TUILoggedOnSAS(
107 IN OUT PGINA_CONTEXT pgContext,
108 IN DWORD dwSasType)
109 {
110 TRACE("TUILoggedOnSAS()\n");
111
112 UNREFERENCED_PARAMETER(pgContext);
113
114 if (dwSasType != WLX_SAS_TYPE_CTRL_ALT_DEL)
115 {
116 /* Nothing to do for WLX_SAS_TYPE_TIMEOUT */
117 return WLX_SAS_ACTION_NONE;
118 }
119
120 FIXME("FIXME: TUILoggedOnSAS(): Let's suppose the user wants to log off...\n");
121 return WLX_SAS_ACTION_LOGOFF;
122 }
123
124 static BOOL
125 ReadString(
126 IN UINT uIdResourcePrompt,
127 IN OUT PWSTR Buffer,
128 IN DWORD BufferLength,
129 IN BOOL ShowString)
130 {
131 DWORD count, i;
132 WCHAR charToDisplay[] = { 0, UNICODE_NULL };
133
134 if (!SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), 0))
135 return FALSE;
136
137 if (!FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE)))
138 return FALSE;
139
140 if (!DisplayResourceText(uIdResourcePrompt, FALSE))
141 return FALSE;
142
143 i = 0;
144 for (;;)
145 {
146 WCHAR readChar;
147 if (!ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), &readChar, 1, &count, NULL))
148 return FALSE;
149 if (readChar == '\r' || readChar == '\n')
150 {
151 /* End of string */
152 charToDisplay[0] = L'\n';
153 WriteConsoleW(
154 GetStdHandle(STD_OUTPUT_HANDLE),
155 charToDisplay,
156 wcslen(charToDisplay),
157 &count,
158 NULL);
159 break;
160 }
161 if (ShowString)
162 {
163 /* Display the char */
164 charToDisplay[0] = readChar;
165 WriteConsoleW(
166 GetStdHandle(STD_OUTPUT_HANDLE),
167 charToDisplay,
168 wcslen(charToDisplay),
169 &count,
170 NULL);
171 }
172 Buffer[i++] = readChar;
173 /* FIXME: buffer overflow if the user writes too many chars! */
174 UNREFERENCED_PARAMETER(BufferLength);
175 /* FIXME: handle backspace */
176 }
177 Buffer[i] = UNICODE_NULL;
178
179 if (!ShowString)
180 {
181 /* Still display the \n */
182 static LPCWSTR newLine = L"\n";
183 DWORD result;
184 WriteConsoleW(
185 GetStdHandle(STD_OUTPUT_HANDLE),
186 newLine,
187 wcslen(newLine),
188 &result,
189 NULL);
190 }
191 return TRUE;
192 }
193
194 static INT
195 TUILoggedOutSAS(
196 IN OUT PGINA_CONTEXT pgContext)
197 {
198 WCHAR UserName[256];
199 WCHAR Password[256];
200
201 TRACE("TUILoggedOutSAS()\n");
202
203 /* Ask the user for credentials */
204 if (!ReadString(IDS_ASKFORUSER, UserName, 256, TRUE))
205 return WLX_SAS_ACTION_NONE;
206 if (!ReadString(IDS_ASKFORPASSWORD, Password, 256, FALSE))
207 return WLX_SAS_ACTION_NONE;
208
209 if (DoLoginTasks(pgContext, UserName, NULL, Password))
210 return WLX_SAS_ACTION_LOGON;
211 else
212 return WLX_SAS_ACTION_NONE;
213 }
214
215 static INT
216 TUILockedSAS(
217 IN OUT PGINA_CONTEXT pgContext)
218 {
219 HANDLE hToken;
220 WCHAR UserName[256];
221 WCHAR Password[256];
222
223 TRACE("TUILockedSAS()\n");
224
225 UNREFERENCED_PARAMETER(pgContext);
226
227 if (!DisplayResourceText(IDS_LOGGEDOUTSAS, TRUE))
228 return WLX_SAS_ACTION_UNLOCK_WKSTA;
229
230 /* Ask the user for credentials */
231 if (!ReadString(IDS_ASKFORUSER, UserName, 256, TRUE))
232 return WLX_SAS_ACTION_NONE;
233 if (!ReadString(IDS_ASKFORPASSWORD, Password, 256, FALSE))
234 return WLX_SAS_ACTION_NONE;
235
236 if (!LogonUserW(UserName, NULL, Password,
237 LOGON32_LOGON_UNLOCK,
238 LOGON32_PROVIDER_DEFAULT,
239 &hToken))
240 {
241 TRACE("LogonUserW() failed\n");
242 return WLX_SAS_ACTION_NONE;
243 }
244 CloseHandle(hToken);
245 return WLX_SAS_ACTION_UNLOCK_WKSTA;
246 }
247
248 static VOID
249 TUIDisplayLockedNotice(
250 IN OUT PGINA_CONTEXT pgContext)
251 {
252 }
253
254 GINA_UI GinaTextUI = {
255 TUIInitialize,
256 TUIDisplayStatusMessage,
257 TUIRemoveStatusMessage,
258 TUIDisplaySASNotice,
259 TUILoggedOnSAS,
260 TUILoggedOutSAS,
261 TUILockedSAS,
262 TUIDisplayLockedNotice,
263 };