xref: /reactos/dll/win32/netid/netid.c (revision 62919904)
1 /*
2  * PROJECT:     ReactOS System Control Panel Applet
3  * LICENSE:     GPL - See COPYING in the top level directory
4  * FILE:        dll/win32/netid/netid.c
5  * PURPOSE:     Network ID Page
6  * COPYRIGHT:   Thomas Weidenmueller <w3seek@reactos.org>
7  *              Dmitry Chapyshev <dmitry@reactos.org>
8  *
9  */
10 
11 #define WIN32_NO_STATUS
12 #include <stdarg.h>
13 #include <windef.h>
14 #include <winbase.h>
15 #include <winuser.h>
16 #include <winreg.h>
17 #include <tchar.h>
18 #include <lm.h>
19 #include <prsht.h>
20 
21 #include "resource.h"
22 
23 
24 #define MAX_COMPUTERDESCRIPTION_LENGTH 256
25 
26 static INT_PTR CALLBACK
27 NetIDPageProc(IN HWND hwndDlg,
28               IN UINT uMsg,
29               IN WPARAM wParam,
30               IN LPARAM lParam);
31 
32 static HINSTANCE hDllInstance;
33 
34 
35 static
36 INT_PTR CALLBACK
37 DNSSuffixPropDlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
38 {
39     switch (Msg)
40     {
41         case WM_COMMAND:
42         {
43             switch (LOWORD(wParam))
44             {
45                 case IDCANCEL:
46                 case IDOK:
47                     EndDialog(hDlg, LOWORD(wParam));
48                     break;
49             }
50         }
51         break;
52     }
53 
54     return FALSE;
55 }
56 
57 static VOID
58 SetRadioBtnState(HWND hDlg, BOOL IsDomain)
59 {
60     SendDlgItemMessage(hDlg, 1008, BM_SETCHECK, (WPARAM)IsDomain, 0);
61     SendDlgItemMessage(hDlg, 1004, BM_SETCHECK, (WPARAM)!IsDomain, 0);
62     EnableWindow(GetDlgItem(hDlg, 116), IsDomain);
63     EnableWindow(GetDlgItem(hDlg, 1007), !IsDomain);
64 }
65 
66 static VOID
67 DisableControls(HWND hDlg)
68 {
69     EnableWindow(GetDlgItem(hDlg, 1008), FALSE);
70     EnableWindow(GetDlgItem(hDlg, 1004), FALSE);
71     EnableWindow(GetDlgItem(hDlg, 116), FALSE);
72     EnableWindow(GetDlgItem(hDlg, 1007), FALSE);
73 }
74 
75 static
76 BOOL
77 IsValidComputerName(LPCWSTR s)
78 {
79     int i;
80 
81     for (i = 0; i <= wcslen(s); i++)
82     {
83         if (s[i] == L'!' || s[i] == L'@' || s[i] == L'#' || s[i] == L'$'
84             || s[i] == L'^' || s[i] == L'&' || s[i] == L'\\' || s[i] == L'|'
85             || s[i] == L')' || s[i] == L'(' || s[i] == L'{' || s[i] == L'"'
86             || s[i] == L'}' || s[i] == L'~' || s[i] == L'/' || s[i] == L'\''
87             || s[i] == L'=' || s[i] == L':' || s[i] == L';' || s[i] == L'+'
88             || s[i] == L'<' || s[i] == L'>' || s[i] == L'?' || s[i] == L'['
89             || s[i] == L']' || s[i] == L'`' || s[i] == L'%' || s[i] == L'_'
90             || s[i] == L'.')
91             return FALSE;
92     }
93 
94     return TRUE;
95 }
96 
97 static
98 INT_PTR CALLBACK
99 NetworkPropDlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
100 {
101     switch (Msg)
102     {
103         case WM_INITDIALOG:
104         {
105             LPWKSTA_INFO_101 wki = NULL;
106             DWORD Size = MAX_COMPUTERNAME_LENGTH + 1;
107             TCHAR ComputerName[MAX_COMPUTERNAME_LENGTH + 1];
108             TCHAR MsgText[MAX_PATH * 2];
109             LPWSTR JoinName;
110             NETSETUP_JOIN_STATUS JoinStatus;
111 
112             if (LoadString(hDllInstance, 25, MsgText, sizeof(MsgText) / sizeof(TCHAR)))
113                 SetDlgItemText(hDlg, 1017, MsgText);
114 
115             SendMessage(GetDlgItem(hDlg, 1002), EM_SETLIMITTEXT, MAX_COMPUTERNAME_LENGTH, 0);
116 
117             if (GetComputerName(ComputerName, &Size))
118             {
119                 SetDlgItemText(hDlg, 1002, ComputerName);
120                 SetDlgItemText(hDlg, 1001, ComputerName);
121             }
122 
123             if (NetGetJoinInformation(NULL, &JoinName, &JoinStatus) != NERR_Success)
124             {
125                 SetRadioBtnState(hDlg, FALSE);
126 
127                 if (NetWkstaGetInfo(NULL,
128                                     101,
129                                     (LPBYTE*)&wki) == NERR_Success)
130                 {
131                     SetDlgItemText(hDlg,
132                                    1007,
133                                    wki->wki101_langroup);
134                 }
135                 else DisableControls(hDlg);
136 
137                 if (wki) NetApiBufferFree(wki);
138             }
139             else
140             {
141                 switch (JoinStatus)
142                 {
143                     case NetSetupDomainName:
144                         SetDlgItemText(hDlg, 116, JoinName);
145                         SetRadioBtnState(hDlg, TRUE);
146                         break;
147 
148                     case NetSetupWorkgroupName:
149                         SetDlgItemText(hDlg, 1007, JoinName);
150                         SetRadioBtnState(hDlg, FALSE);
151                         break;
152 
153                     case NetSetupUnjoined:
154                         break;
155 
156                     case NetSetupUnknownStatus:
157                     default:
158                     {
159                         SetRadioBtnState(hDlg, FALSE);
160 
161                         if (NetWkstaGetInfo(NULL,
162                                             101,
163                                             (LPBYTE*)&wki) == NERR_Success)
164                         {
165                             SetDlgItemText(hDlg,
166                                            1007,
167                                            wki->wki101_langroup);
168                         }
169                         else DisableControls(hDlg);
170 
171                         if (wki) NetApiBufferFree(wki);
172                         break;
173                     }
174                 }
175 
176                 if (JoinName) NetApiBufferFree(JoinName);
177             }
178         }
179         break;
180 
181         case WM_COMMAND:
182         {
183             switch (LOWORD(wParam))
184             {
185                 case 1002:
186                     if (HIWORD(wParam) == EN_CHANGE)
187                     {
188                         TCHAR szText[MAX_COMPUTERNAME_LENGTH + 1];
189 
190                         GetWindowText(GetDlgItem(hDlg, 1002), szText, MAX_COMPUTERNAME_LENGTH + 1);
191                         SetDlgItemText(hDlg, 1001, szText);
192                     }
193                     break;
194 
195                 case 1008: /* Domain radio button */
196                 case 1004: /* Workgroup radio button */
197                     if (SendDlgItemMessage(hDlg, 1008, BM_GETCHECK, 0, 0) == BST_CHECKED)
198                         SetRadioBtnState(hDlg, TRUE);
199                     else
200                         SetRadioBtnState(hDlg, FALSE);
201                     break;
202 
203                 case 1003:
204                     DialogBox(hDllInstance,
205                               MAKEINTRESOURCE(IDD_PROPPAGEDNSANDNETBIOS),
206                               hDlg,
207                               DNSSuffixPropDlgProc);
208                     break;
209 
210                 case IDOK:
211                 {
212                     DWORD Size = MAX_COMPUTERNAME_LENGTH + 1;
213                     TCHAR ComputerName[MAX_COMPUTERNAME_LENGTH + 1];
214                     TCHAR NewComputerName[MAX_COMPUTERNAME_LENGTH + 1];
215 
216                     if (GetComputerName(ComputerName, &Size))
217                     {
218                         if (GetWindowText(GetDlgItem(hDlg, 1002),
219                                           NewComputerName,
220                                           (MAX_COMPUTERNAME_LENGTH + 1)))
221                         {
222                             if (_tcscmp(ComputerName, NewComputerName) != 0)
223                             {
224                                 if (!IsValidComputerName(NewComputerName))
225                                 {
226                                     TCHAR szText[MAX_PATH], szMsgText[MAX_PATH];
227 
228                                     LoadString(hDllInstance, 1030, szText, sizeof(szText) / sizeof(TCHAR));
229 
230                                     swprintf(szMsgText, szText, NewComputerName);
231                                     MessageBox(hDlg, szMsgText, NULL, MB_OK | MB_ICONERROR);
232                                     SetFocus(GetDlgItem(hDlg, 1002));
233                                     break;
234                                 }
235                                 else if (!SetComputerNameExW(ComputerNamePhysicalDnsHostname, NewComputerName))
236                                 {
237                                     TCHAR szMsgText[MAX_PATH];
238 
239                                     LoadString(hDllInstance, 4001, szMsgText, sizeof(szMsgText) / sizeof(TCHAR));
240 
241                                     MessageBox(hDlg, szMsgText, NULL, MB_OK | MB_ICONERROR);
242                                 }
243                                 else
244                                 {
245                                     TCHAR szMsgTitle[MAX_PATH], szMsgText[MAX_PATH];
246 
247                                     LoadString(hDllInstance, 4000, szMsgTitle, sizeof(szMsgTitle) / sizeof(TCHAR));
248                                     LoadString(hDllInstance, 24, szMsgText, sizeof(szMsgText) / sizeof(TCHAR));
249 
250                                     MessageBox(hDlg, szMsgText, szMsgTitle, MB_OK | MB_ICONINFORMATION);
251 
252                                     NetIDPageProc(GetParent(hDlg), WM_INITDIALOG, 0, 0);
253                                 }
254                             }
255                         }
256                     }
257 
258                     EndDialog(hDlg, LOWORD(wParam));
259                 }
260                 break;
261 
262                 case IDCANCEL:
263                     EndDialog(hDlg, LOWORD(wParam));
264                     break;
265             }
266         }
267         break;
268     }
269 
270     return FALSE;
271 }
272 
273 static
274 VOID
275 NetIDPage_OnInitDialog(
276     HWND hwndDlg)
277 {
278     WCHAR ComputerDescription[MAX_COMPUTERDESCRIPTION_LENGTH + 1];
279     WCHAR ComputerName[MAX_COMPUTERNAME_LENGTH + 1];
280     DWORD RegSize = sizeof(ComputerDescription);
281     DWORD Size = MAX_COMPUTERNAME_LENGTH + 1;
282     HKEY KeyHandle;
283     LPWKSTA_INFO_101 wki;
284     LONG lError;
285 
286     /* Display computer name and description */
287     SendDlgItemMessage(hwndDlg, IDC_COMPDESC, EM_SETLIMITTEXT, MAX_COMPUTERDESCRIPTION_LENGTH, 0);
288 
289     lError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
290                            L"SYSTEM\\CurrentControlSet\\Services\\LanmanServer\\Parameters",
291                            0,
292                            KEY_QUERY_VALUE,
293                            &KeyHandle);
294     if (lError == ERROR_SUCCESS)
295     {
296         lError = RegQueryValueExW(KeyHandle,
297                                   L"srvcomment",
298                                   0,
299                                   NULL,
300                                   (LPBYTE)ComputerDescription,
301                                   &RegSize);
302         if (lError == ERROR_SUCCESS)
303         {
304             ComputerDescription[RegSize / sizeof(WCHAR)] = UNICODE_NULL;
305             SetDlgItemText(hwndDlg, IDC_COMPDESC, ComputerDescription);
306         }
307 
308         RegCloseKey(KeyHandle);
309     }
310 
311     if (GetComputerName(ComputerName, &Size))
312     {
313         SetDlgItemText(hwndDlg, IDC_COMPUTERNAME, ComputerName);
314     }
315 
316     if (NetWkstaGetInfo(NULL, 101, (LPBYTE*)&wki) == NERR_Success)
317     {
318         SetDlgItemText(hwndDlg, IDC_WORKGROUPDOMAIN_NAME, wki->wki101_langroup);
319         NetApiBufferFree(wki);
320     }
321 }
322 
323 static
324 LONG
325 NetIDPage_OnApply(
326     HWND hwndDlg)
327 {
328     WCHAR ComputerDescription[MAX_COMPUTERDESCRIPTION_LENGTH + 1];
329     WCHAR NewComputerDescription[MAX_COMPUTERDESCRIPTION_LENGTH + 1];
330     HKEY KeyHandle = NULL;
331     DWORD dwSize;
332     LONG lError;
333 
334     lError = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
335                            L"SYSTEM\\CurrentControlSet\\Services\\LanmanServer\\Parameters",
336                            0,
337                            KEY_QUERY_VALUE | KEY_SET_VALUE,
338                            &KeyHandle);
339     if (lError != ERROR_SUCCESS)
340         return lError;
341 
342     GetDlgItemTextW(hwndDlg,
343                     IDC_COMPDESC,
344                     NewComputerDescription,
345                     ARRAYSIZE(NewComputerDescription));
346     if (GetLastError() != ERROR_SUCCESS)
347     {
348         lError = GetLastError();
349         goto done;
350     }
351 
352     dwSize = sizeof(ComputerDescription);
353     lError = RegQueryValueExW(KeyHandle,
354                               L"srvcomment",
355                               0,
356                               NULL,
357                               (PBYTE)ComputerDescription,
358                               &dwSize);
359     if (lError != ERROR_SUCCESS && lError != ERROR_FILE_NOT_FOUND)
360         goto done;
361 
362     lError = ERROR_SUCCESS;
363     if (wcscmp(ComputerDescription, NewComputerDescription) != 0)
364     {
365         lError = RegSetValueExW(KeyHandle,
366                                 L"srvcomment",
367                                 0,
368                                 REG_SZ,
369                                 (PBYTE)NewComputerDescription,
370                                 (wcslen(NewComputerDescription) + 1) * sizeof(WCHAR));
371     }
372 
373 done:
374     if (KeyHandle != NULL)
375         RegCloseKey(KeyHandle);
376 
377     return lError;
378 }
379 
380 static INT_PTR CALLBACK
381 NetIDPageProc(IN HWND hwndDlg,
382               IN UINT uMsg,
383               IN WPARAM wParam,
384               IN LPARAM lParam)
385 {
386     static BOOL bEnable = FALSE;
387     INT_PTR Ret = 0;
388 
389     switch (uMsg)
390     {
391         case WM_INITDIALOG:
392             NetIDPage_OnInitDialog(hwndDlg);
393             bEnable = TRUE;
394             Ret = TRUE;
395             break;
396 
397         case WM_NOTIFY:
398             switch (((LPNMHDR)lParam)->code)
399             {
400                 case PSN_APPLY:
401                     NetIDPage_OnApply(hwndDlg);
402                     break;
403             }
404             break;
405 
406         case WM_COMMAND:
407             switch (LOWORD(wParam))
408             {
409                 case IDC_COMPDESC:
410                     if (HIWORD(wParam) == EN_CHANGE && bEnable == TRUE)
411                         PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
412                     break;
413 
414                 case IDC_NETWORK_PROPERTY:
415                     DialogBox(hDllInstance,
416                               MAKEINTRESOURCE(IDD_PROPPAGECOMPNAMECHENGE),
417                               hwndDlg,
418                               NetworkPropDlgProc);
419                     break;
420             }
421             break;
422     }
423 
424     return Ret;
425 }
426 
427 HPROPSHEETPAGE WINAPI
428 CreateNetIDPropertyPage(VOID)
429 {
430     PROPSHEETPAGE psp = {0};
431 
432     psp.dwSize = sizeof(psp);
433     psp.dwFlags = PSP_DEFAULT;
434     psp.hInstance = hDllInstance;
435     psp.pszTemplate = MAKEINTRESOURCE(IDD_PROPPAGECOMPUTER);
436     psp.pfnDlgProc = NetIDPageProc;
437 
438     return CreatePropertySheetPage(&psp);
439 }
440 
441 BOOL WINAPI
442 DllMain(IN HINSTANCE hinstDLL,
443         IN DWORD dwReason,
444         IN LPVOID lpvReserved)
445 {
446     switch (dwReason)
447     {
448         case DLL_PROCESS_ATTACH:
449             hDllInstance = hinstDLL;
450             DisableThreadLibraryCalls(hinstDLL);
451             break;
452     }
453 
454     return TRUE;
455 }
456