e8694514050f3cb714bcf315df1d8d1fe8155e8b
[reactos.git] / reactos / base / applications / mscutils / servman / propsheet_logon.c
1 /*
2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/mscutils/servman/propsheet_logon.c
5 * PURPOSE: Logon property page
6 * COPYRIGHT: Eric Kohl
7 */
8
9 #include "precomp.h"
10
11 #define NDEBUG
12 #include <debug.h>
13
14 #define DEFAULT_PASSWORD L" "
15
16 typedef struct _LOGONDATA
17 {
18 ENUM_SERVICE_STATUS_PROCESS *pService;
19 LPQUERY_SERVICE_CONFIG pServiceConfig;
20 WCHAR szAccountName[64];
21 WCHAR szPassword1[64];
22 WCHAR szPassword2[64];
23 BOOL bInitialized;
24 BOOL bLocalSystem;
25 BOOL bAccountChanged;
26 } LOGONDATA, *PLOGONDATA;
27
28
29 static
30 VOID
31 SetControlStates(
32 HWND hwndDlg,
33 PLOGONDATA pLogonData,
34 BOOL bLocalSystem)
35 {
36 EnableWindow(GetDlgItem(hwndDlg, IDC_LOGON_INTERACTIVE), bLocalSystem);
37 EnableWindow(GetDlgItem(hwndDlg, IDC_LOGON_ACCOUNTNAME), !bLocalSystem);
38 EnableWindow(GetDlgItem(hwndDlg, IDC_LOGON_SEARCH), FALSE /*!bLocalSystem*/);
39 EnableWindow(GetDlgItem(hwndDlg, IDC_LOGON_PW1TEXT), !bLocalSystem);
40 EnableWindow(GetDlgItem(hwndDlg, IDC_LOGON_PASSWORD1), !bLocalSystem);
41 EnableWindow(GetDlgItem(hwndDlg, IDC_LOGON_PW2TEXT), !bLocalSystem);
42 EnableWindow(GetDlgItem(hwndDlg, IDC_LOGON_PASSWORD2), !bLocalSystem);
43
44 if (bLocalSystem)
45 {
46 if (pLogonData->bInitialized == TRUE)
47 {
48 GetDlgItemText(hwndDlg, IDC_LOGON_ACCOUNTNAME, pLogonData->szAccountName, 64);
49 GetDlgItemText(hwndDlg, IDC_LOGON_PASSWORD1, pLogonData->szPassword1, 64);
50 GetDlgItemText(hwndDlg, IDC_LOGON_PASSWORD2, pLogonData->szPassword2, 64);
51 }
52
53 SetDlgItemText(hwndDlg, IDC_LOGON_ACCOUNTNAME, L"");
54 SetDlgItemText(hwndDlg, IDC_LOGON_PASSWORD1, L"");
55 SetDlgItemText(hwndDlg, IDC_LOGON_PASSWORD2, L"");
56 }
57 else
58 {
59 SetDlgItemText(hwndDlg, IDC_LOGON_ACCOUNTNAME, pLogonData->szAccountName);
60 SetDlgItemText(hwndDlg, IDC_LOGON_PASSWORD1, pLogonData->szPassword1);
61 SetDlgItemText(hwndDlg, IDC_LOGON_PASSWORD2, pLogonData->szPassword2);
62 }
63
64 pLogonData->bLocalSystem = bLocalSystem;
65 }
66
67
68 #if 0
69 BOOL
70 SetServiceAccount(
71 LPWSTR lpServiceName,
72 LPWSTR lpStartName,
73 LPWSTR lpPassword)
74 {
75 SC_HANDLE hSCManager;
76 SC_HANDLE hSc;
77 SC_LOCK scLock;
78 BOOL bRet = FALSE;
79
80 hSCManager = OpenSCManagerW(NULL,
81 NULL,
82 SC_MANAGER_LOCK);
83 if (hSCManager)
84 {
85 scLock = LockServiceDatabase(hSCManager);
86 if (scLock)
87 {
88 hSc = OpenServiceW(hSCManager,
89 lpServiceName,
90 SERVICE_CHANGE_CONFIG);
91 if (hSc)
92 {
93 if (ChangeServiceConfigW(hSc,
94 SERVICE_NO_CHANGE,
95 SERVICE_NO_CHANGE,
96 SERVICE_NO_CHANGE,
97 NULL,
98 NULL,
99 NULL,
100 NULL,
101 lpStartName,
102 lpPassword,
103 NULL))
104 {
105 bRet = TRUE;
106 }
107
108 CloseServiceHandle(hSc);
109 }
110
111 UnlockServiceDatabase(scLock);
112 }
113
114 CloseServiceHandle(hSCManager);
115 }
116
117 if (!bRet)
118 GetError();
119
120 return bRet;
121 }
122 #endif
123
124
125 static
126 BOOL
127 OnQueryInitialFocus(
128 HWND hwndDlg,
129 PLOGONDATA pLogonData)
130 {
131 HWND hwnd = GetDlgItem(hwndDlg, pLogonData->bLocalSystem ? IDC_LOGON_SYSTEMACCOUNT : IDC_LOGON_THISACCOUNT);
132
133 SetWindowLong(hwndDlg, DWL_MSGRESULT, (LPARAM)hwnd);
134
135 return TRUE;
136 }
137
138
139 static
140 BOOL
141 OnApply(
142 HWND hwndDlg,
143 PLOGONDATA pLogonData)
144 {
145 WCHAR szAccountName[64];
146 WCHAR szPassword1[64];
147 WCHAR szPassword2[64];
148 BOOL bRet = TRUE;
149
150 if (!pLogonData->bAccountChanged)
151 return TRUE;
152
153 if (SendDlgItemMessageW(hwndDlg, IDC_LOGON_SYSTEMACCOUNT, BM_GETCHECK, 0, 0) == BST_CHECKED)
154 {
155 /* System account selected */
156 wcscpy(szAccountName, L"LocalSystem");
157 wcscpy(szPassword1, L"");
158 wcscpy(szPassword2, L"");
159 }
160 else
161 {
162 /* Other account selected */
163 GetDlgItemText(hwndDlg, IDC_LOGON_ACCOUNTNAME, szAccountName, 64);
164 GetDlgItemText(hwndDlg, IDC_LOGON_PASSWORD1, szPassword1, 64);
165 GetDlgItemText(hwndDlg, IDC_LOGON_PASSWORD2, szPassword2, 64);
166
167 if (wcscmp(szPassword1, szPassword2))
168 {
169 ResourceMessageBox(GetModuleHandle(NULL), hwndDlg, MB_OK | MB_ICONWARNING, IDS_APPNAME, IDS_NOT_SAME_PASSWORD);
170 return FALSE;
171 }
172
173 if (!wcscmp(szPassword1, DEFAULT_PASSWORD))
174 {
175 ResourceMessageBox(GetModuleHandle(NULL), hwndDlg, MB_OK | MB_ICONWARNING, IDS_APPNAME, IDS_INVALID_PASSWORD);
176 return FALSE;
177 }
178
179
180 }
181
182 #if 0
183 bRet = SetServiceAccount(pLogonData->pService->lpServiceName,
184 szAccountName,
185 szPassword1);
186 if (bRet == FALSE)
187 {
188
189 }
190 #endif
191
192 if (bRet == TRUE)
193 pLogonData->bAccountChanged = FALSE;
194
195 return bRet;
196 }
197
198
199 /*
200 * Logon Property dialog callback.
201 * Controls messages to the Logon dialog
202 */
203 INT_PTR
204 CALLBACK
205 LogonPageProc(
206 HWND hwndDlg,
207 UINT uMsg,
208 WPARAM wParam,
209 LPARAM lParam)
210 {
211 PLOGONDATA pLogonData;
212
213 /* Get the window context */
214 pLogonData = (PLOGONDATA)GetWindowLongPtr(hwndDlg,
215 GWLP_USERDATA);
216 if (pLogonData == NULL && uMsg != WM_INITDIALOG)
217 {
218 return FALSE;
219 }
220
221 switch (uMsg)
222 {
223 case WM_INITDIALOG:
224 pLogonData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(LOGONDATA));
225 if (pLogonData != NULL)
226 {
227 SetWindowLongPtr(hwndDlg,
228 GWLP_USERDATA,
229 (LONG_PTR)pLogonData);
230
231 pLogonData->bInitialized = FALSE;
232 pLogonData->pService = ((PSERVICEPROPSHEET)(((LPPROPSHEETPAGE)lParam)->lParam))->pService;
233
234 pLogonData->pServiceConfig = GetServiceConfig(pLogonData->pService->lpServiceName);
235 if (pLogonData->pServiceConfig != NULL)
236 {
237 wcscpy(pLogonData->szPassword1, DEFAULT_PASSWORD);
238 wcscpy(pLogonData->szPassword2, DEFAULT_PASSWORD);
239
240 if (pLogonData->pServiceConfig->lpServiceStartName == NULL ||
241 _wcsicmp(pLogonData->pServiceConfig->lpServiceStartName, L"LocalSystem") == 0)
242 {
243 SendMessageW(GetDlgItem(hwndDlg, IDC_LOGON_SYSTEMACCOUNT), BM_SETCHECK, (WPARAM)BST_CHECKED, 0);
244 SetControlStates(hwndDlg, pLogonData, TRUE);
245 }
246 else
247 {
248 wcscpy(pLogonData->szAccountName, pLogonData->pServiceConfig->lpServiceStartName);
249 SendMessageW(GetDlgItem(hwndDlg, IDC_LOGON_THISACCOUNT), BM_SETCHECK, (WPARAM)BST_CHECKED, 0);
250 SetControlStates(hwndDlg, pLogonData, FALSE);
251 }
252 }
253
254 pLogonData->bInitialized = TRUE;
255 }
256
257 EnableWindow(GetDlgItem(hwndDlg, IDC_LOGON_HWPROFILE), FALSE);
258 break;
259
260 case WM_DESTROY:
261 if (pLogonData->pServiceConfig)
262 HeapFree(GetProcessHeap(), 0, pLogonData->pServiceConfig);
263
264 HeapFree(GetProcessHeap(), 0, pLogonData);
265 break;
266
267 case WM_COMMAND:
268 switch(LOWORD(wParam))
269 {
270 case IDC_LOGON_SYSTEMACCOUNT:
271 if (HIWORD(wParam) == BN_CLICKED)
272 {
273 if (pLogonData->bInitialized)
274 {
275 pLogonData->bAccountChanged = TRUE;
276 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
277 }
278 SetControlStates(hwndDlg, pLogonData, TRUE);
279 }
280 break;
281
282 case IDC_LOGON_THISACCOUNT:
283 if (HIWORD(wParam) == BN_CLICKED)
284 {
285 if (pLogonData->bInitialized)
286 {
287 pLogonData->bAccountChanged = TRUE;
288 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
289 }
290 SetControlStates(hwndDlg, pLogonData, FALSE);
291 }
292 break;
293
294 case IDC_LOGON_ACCOUNTNAME:
295 case IDC_LOGON_PASSWORD1:
296 case IDC_LOGON_PASSWORD2:
297 if (HIWORD(wParam) == EN_CHANGE && pLogonData->bInitialized)
298 {
299 pLogonData->bAccountChanged = TRUE;
300 PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
301 }
302 break;
303 }
304 break;
305
306 case WM_NOTIFY:
307 switch (((LPNMHDR)lParam)->code)
308 {
309 case PSN_QUERYINITIALFOCUS :
310 return OnQueryInitialFocus(hwndDlg, pLogonData);
311
312 case PSN_APPLY:
313 return OnApply(hwndDlg, pLogonData);
314 }
315 break;
316 }
317
318 return FALSE;
319 }