xref: /reactos/dll/win32/netid/netid.c (revision 2b82fe44)
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 #include <windows.h>
12 #include <tchar.h>
13 #include <lm.h>
14 #include <prsht.h>
15 #include "resource.h"
16 
17 static INT_PTR CALLBACK
18 NetIDPageProc(IN HWND hwndDlg,
19               IN UINT uMsg,
20               IN WPARAM wParam,
21               IN LPARAM lParam);
22 
23 static HINSTANCE hDllInstance;
24 
25 
26 static
27 INT_PTR CALLBACK
28 DNSSuffixPropDlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
29 {
30     switch (Msg)
31     {
32         case WM_COMMAND:
33         {
34             switch (LOWORD(wParam))
35             {
36                 case IDCANCEL:
37                 case IDOK:
38                     EndDialog(hDlg, LOWORD(wParam));
39                     break;
40             }
41         }
42         break;
43     }
44 
45     return FALSE;
46 }
47 
48 static VOID
49 SetRadioBtnState(HWND hDlg, BOOL IsDomain)
50 {
51     SendDlgItemMessage(hDlg, 1008, BM_SETCHECK, (WPARAM)IsDomain, 0);
52     SendDlgItemMessage(hDlg, 1004, BM_SETCHECK, (WPARAM)!IsDomain, 0);
53     EnableWindow(GetDlgItem(hDlg, 116), IsDomain);
54     EnableWindow(GetDlgItem(hDlg, 1007), !IsDomain);
55 }
56 
57 static VOID
58 DisableControls(HWND hDlg)
59 {
60     EnableWindow(GetDlgItem(hDlg, 1008), FALSE);
61     EnableWindow(GetDlgItem(hDlg, 1004), FALSE);
62     EnableWindow(GetDlgItem(hDlg, 116), FALSE);
63     EnableWindow(GetDlgItem(hDlg, 1007), FALSE);
64 }
65 
66 static
67 BOOL
68 IsValidComputerName(LPCWSTR s)
69 {
70     int i;
71 
72     for (i = 0; i <= wcslen(s); i++)
73     {
74         if (s[i] == L'!' || s[i] == L'@' || s[i] == L'#' || s[i] == L'$'
75             || s[i] == L'^' || s[i] == L'&' || s[i] == L'\\' || s[i] == L'|'
76             || s[i] == L')' || s[i] == L'(' || s[i] == L'{' || s[i] == L'"'
77             || s[i] == L'}' || s[i] == L'~' || s[i] == L'/' || s[i] == L'\''
78             || s[i] == L'=' || s[i] == L':' || s[i] == L';' || s[i] == L'+'
79             || s[i] == L'<' || s[i] == L'>' || s[i] == L'?' || s[i] == L'['
80             || s[i] == L']' || s[i] == L'`' || s[i] == L'%' || s[i] == L'_'
81             || s[i] == L'.')
82             return FALSE;
83     }
84 
85     return TRUE;
86 }
87 
88 static
89 INT_PTR CALLBACK
90 NetworkPropDlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
91 {
92     switch (Msg)
93     {
94         case WM_INITDIALOG:
95         {
96             LPWKSTA_INFO_101 wki = NULL;
97             DWORD Size = MAX_COMPUTERNAME_LENGTH + 1;
98             TCHAR ComputerName[MAX_COMPUTERNAME_LENGTH + 1];
99             TCHAR MsgText[MAX_PATH * 2];
100             LPWSTR JoinName;
101             NETSETUP_JOIN_STATUS JoinStatus;
102 
103             if (LoadString(hDllInstance, 25, MsgText, sizeof(MsgText) / sizeof(TCHAR)))
104                 SetDlgItemText(hDlg, 1017, MsgText);
105 
106             SendMessage(GetDlgItem(hDlg, 1002), EM_SETLIMITTEXT, MAX_COMPUTERNAME_LENGTH, 0);
107 
108             if (GetComputerName(ComputerName, &Size))
109             {
110                 SetDlgItemText(hDlg, 1002, ComputerName);
111                 SetDlgItemText(hDlg, 1001, ComputerName);
112             }
113 
114             if (NetGetJoinInformation(NULL, &JoinName, &JoinStatus) != NERR_Success)
115             {
116                 SetRadioBtnState(hDlg, FALSE);
117 
118                 if (NetWkstaGetInfo(NULL,
119                                     101,
120                                     (LPBYTE*)&wki) == NERR_Success)
121                 {
122                     SetDlgItemText(hDlg,
123                                    1007,
124                                    wki->wki101_langroup);
125                 }
126                 else DisableControls(hDlg);
127 
128                 if (wki) NetApiBufferFree(wki);
129             }
130             else
131             {
132                 switch (JoinStatus)
133                 {
134                     case NetSetupDomainName:
135                         SetDlgItemText(hDlg, 116, JoinName);
136                         SetRadioBtnState(hDlg, TRUE);
137                         break;
138 
139                     case NetSetupWorkgroupName:
140                         SetDlgItemText(hDlg, 1007, JoinName);
141                         SetRadioBtnState(hDlg, FALSE);
142                         break;
143 
144                     case NetSetupUnjoined:
145                         break;
146 
147                     case NetSetupUnknownStatus:
148                     default:
149                     {
150                         SetRadioBtnState(hDlg, FALSE);
151 
152                         if (NetWkstaGetInfo(NULL,
153                                             101,
154                                             (LPBYTE*)&wki) == NERR_Success)
155                         {
156                             SetDlgItemText(hDlg,
157                                            1007,
158                                            wki->wki101_langroup);
159                         }
160                         else DisableControls(hDlg);
161 
162                         if (wki) NetApiBufferFree(wki);
163                         break;
164                     }
165                 }
166 
167                 if (JoinName) NetApiBufferFree(JoinName);
168             }
169         }
170         break;
171 
172         case WM_COMMAND:
173         {
174             switch (LOWORD(wParam))
175             {
176                 case 1002:
177                     if (HIWORD(wParam) == EN_CHANGE)
178                     {
179                         TCHAR szText[MAX_COMPUTERNAME_LENGTH + 1];
180 
181                         GetWindowText(GetDlgItem(hDlg, 1002), szText, MAX_COMPUTERNAME_LENGTH + 1);
182                         SetDlgItemText(hDlg, 1001, szText);
183                     }
184                     break;
185 
186                 case 1008: /* Domain radio button */
187                 case 1004: /* Workgroup radio button */
188                     if (SendDlgItemMessage(hDlg, 1008, BM_GETCHECK, 0, 0) == BST_CHECKED)
189                         SetRadioBtnState(hDlg, TRUE);
190                     else
191                         SetRadioBtnState(hDlg, FALSE);
192                     break;
193 
194                 case 1003:
195                     DialogBox(hDllInstance,
196                               MAKEINTRESOURCE(IDD_PROPPAGEDNSANDNETBIOS),
197                               hDlg,
198                               DNSSuffixPropDlgProc);
199                     break;
200 
201                 case IDOK:
202                 {
203                     DWORD Size = MAX_COMPUTERNAME_LENGTH + 1;
204                     TCHAR ComputerName[MAX_COMPUTERNAME_LENGTH + 1];
205                     TCHAR NewComputerName[MAX_COMPUTERNAME_LENGTH + 1];
206 
207                     if (GetComputerName(ComputerName, &Size))
208                     {
209                         if (GetWindowText(GetDlgItem(hDlg, 1002),
210                                           NewComputerName,
211                                           (MAX_COMPUTERNAME_LENGTH + 1)))
212                         {
213                             if (_tcscmp(ComputerName, NewComputerName) != 0)
214                             {
215                                 if (!IsValidComputerName(NewComputerName))
216                                 {
217                                     TCHAR szText[MAX_PATH], szMsgText[MAX_PATH];
218 
219                                     LoadString(hDllInstance, 1030, szText, sizeof(szText) / sizeof(TCHAR));
220 
221                                     swprintf(szMsgText, szText, NewComputerName);
222                                     MessageBox(hDlg, szMsgText, NULL, MB_OK | MB_ICONERROR);
223                                     SetFocus(GetDlgItem(hDlg, 1002));
224                                     break;
225                                 }
226                                 else if (!SetComputerName(NewComputerName))
227                                 {
228                                     TCHAR szMsgText[MAX_PATH];
229 
230                                     LoadString(hDllInstance, 4001, szMsgText, sizeof(szMsgText) / sizeof(TCHAR));
231 
232                                     MessageBox(hDlg, szMsgText, NULL, MB_OK | MB_ICONERROR);
233                                 }
234                                 else
235                                 {
236                                     TCHAR szMsgTitle[MAX_PATH], szMsgText[MAX_PATH];
237 
238                                     LoadString(hDllInstance, 4000, szMsgTitle, sizeof(szMsgTitle) / sizeof(TCHAR));
239                                     LoadString(hDllInstance, 24, szMsgText, sizeof(szMsgText) / sizeof(TCHAR));
240 
241                                     MessageBox(hDlg, szMsgText, szMsgTitle, MB_OK | MB_ICONINFORMATION);
242 
243                                     NetIDPageProc(GetParent(hDlg), WM_INITDIALOG, 0, 0);
244                                 }
245                             }
246                         }
247                     }
248 
249                     EndDialog(hDlg, LOWORD(wParam));
250                 }
251                 break;
252 
253                 case IDCANCEL:
254                     EndDialog(hDlg, LOWORD(wParam));
255                     break;
256             }
257         }
258         break;
259     }
260 
261     return FALSE;
262 }
263 
264 static INT_PTR CALLBACK
265 NetIDPageProc(IN HWND hwndDlg,
266               IN UINT uMsg,
267               IN WPARAM wParam,
268               IN LPARAM lParam)
269 {
270     INT_PTR Ret = 0;
271 
272     UNREFERENCED_PARAMETER(lParam);
273 
274     switch (uMsg)
275     {
276         case WM_INITDIALOG:
277         {
278             /* Display computer name */
279             LPWKSTA_INFO_101 wki = NULL;
280             DWORD Size = MAX_COMPUTERNAME_LENGTH + 1;
281             TCHAR ComputerName[MAX_COMPUTERNAME_LENGTH + 1];
282             if (GetComputerName(ComputerName,&Size))
283             {
284                 SetDlgItemText(hwndDlg,
285                                IDC_COMPUTERNAME,
286                                ComputerName);
287             }
288             if (NetWkstaGetInfo(NULL,
289                                 101,
290                                 (LPBYTE*)&wki) == NERR_Success)
291             {
292                 SetDlgItemText(hwndDlg,
293                                IDC_WORKGROUPDOMAIN_NAME,
294                                wki->wki101_langroup);
295             }
296 
297             if (wki) NetApiBufferFree(wki);
298 
299             Ret = TRUE;
300             break;
301         }
302 
303         case WM_COMMAND:
304             switch (LOWORD(wParam))
305             {
306                 case IDC_NETWORK_PROPERTY:
307                     DialogBox(hDllInstance,
308                               MAKEINTRESOURCE(IDD_PROPPAGECOMPNAMECHENGE),
309                               hwndDlg,
310                               NetworkPropDlgProc);
311                 break;
312             }
313             break;
314     }
315 
316     return Ret;
317 }
318 
319 HPROPSHEETPAGE WINAPI
320 CreateNetIDPropertyPage(VOID)
321 {
322     PROPSHEETPAGE psp = {0};
323 
324     psp.dwSize = sizeof(psp);
325     psp.dwFlags = PSP_DEFAULT;
326     psp.hInstance = hDllInstance;
327     psp.pszTemplate = MAKEINTRESOURCE(IDD_PROPPAGECOMPUTER);
328     psp.pfnDlgProc = NetIDPageProc;
329 
330     return CreatePropertySheetPage(&psp);
331 }
332 
333 BOOL WINAPI
334 DllMain(IN HINSTANCE hinstDLL,
335         IN DWORD dwReason,
336         IN LPVOID lpvReserved)
337 {
338     switch (dwReason)
339     {
340         case DLL_PROCESS_ATTACH:
341             hDllInstance = hinstDLL;
342             DisableThreadLibraryCalls(hinstDLL);
343             break;
344     }
345 
346     return TRUE;
347 }
348