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