Merge HAL changes 34743, 34812, 34839, 34917, 35515, 35771, 35902, 35904,
[reactos.git] / rosapps / applications / sysutils / gettype / gettype.c
1 /*
2 * ReactOS Win32 Applications
3 * Copyright (C) 2005 ReactOS Team
4 *
5 * COPYRIGHT: See COPYING in the top level directory
6 * PROJECT: ReactOS arp utility
7 * FILE: apps/utils/gettype/gettype.c
8 * PURPOSE:
9 * PROGRAMMERS: Brandon Turner (turnerb7@msu.edu)
10 * REVISIONS:
11 * GM 30/10/05 Created
12 *
13 * FIXME: gettype only supports local computer.
14 */
15 #include <windows.h>
16 #include <tchar.h>
17 #include <stdio.h>
18 #include <lm.h>
19 #include <shellapi.h>
20
21 enum
22 {
23 GETTYPE_ROLE = 0x001,
24 GETTYPE_HELP = 0x002,
25 GETTYPE_SP = 0x004,
26 GETTYPE_VER = 0x008,
27 GETTYPE_MINV = 0x010,
28 GETTYPE_MAJV = 0x020,
29 GETTYPE_TYPE = 0x040,
30 GETTYPE_BUILD = 0x080
31 };
32
33 INT
34 GetVersionNumber(BOOL bLocal, LPOSVERSIONINFOEX osvi, LPSERVER_INFO_102 pBuf102)
35 {
36 INT VersionNumber = 255;
37 if(pBuf102 != NULL && !bLocal)
38 {
39 VersionNumber = pBuf102->sv102_version_major * 1000;
40 VersionNumber += (pBuf102->sv102_version_minor * 100);
41 }
42 else if(bLocal)
43 {
44 VersionNumber = osvi->dwMajorVersion * 1000;
45 VersionNumber += (osvi->dwMinorVersion * 100);
46 }
47
48 return VersionNumber;
49 }
50
51 INT
52 GetMajValue(BOOL Major, BOOL bLocal, LPOSVERSIONINFOEX osvi, LPSERVER_INFO_102 pBuf102)
53 {
54 INT VersionNumber = 255;
55 if(pBuf102 != NULL && !bLocal)
56 {
57 if(Major)
58 VersionNumber = pBuf102->sv102_version_major * 1000;
59 else
60 VersionNumber = pBuf102->sv102_version_minor * 100;
61 }
62 else
63 {
64 if(Major)
65 VersionNumber = osvi->dwMajorVersion * 1000;
66 else
67 VersionNumber = osvi->dwMinorVersion * 100;
68 }
69 return VersionNumber;
70 }
71
72 INT
73 GetSystemRole(BOOL bLocal, LPOSVERSIONINFOEX osvi, LPSERVER_INFO_102 pBuf102)
74 {
75
76 if(pBuf102 != NULL && !bLocal)
77 {
78 if ((pBuf102->sv102_type & SV_TYPE_DOMAIN_CTRL) ||
79 (pBuf102->sv102_type & SV_TYPE_DOMAIN_BAKCTRL))
80 return 1;
81 else if(pBuf102->sv102_type & SV_TYPE_SERVER_NT)
82 return 2;
83 else
84 return 3;
85 }
86 else
87 {
88 if(osvi->wProductType == VER_NT_DOMAIN_CONTROLLER)
89 return 1;
90 else if(osvi->wProductType == VER_NT_SERVER)
91 return 2;
92 else if(osvi->wProductType == VER_NT_WORKSTATION)
93 return 3;
94 }
95 return 255;
96
97 }
98
99 INT
100 GetServicePack(BOOL bLocal, LPOSVERSIONINFOEX osvi, LPSERVER_INFO_102 pBuf102, TCHAR * Server)
101 {
102 INT SPNumber = 255;
103 if(!bLocal)
104 {
105 /* FIXME: Use Registry to get value */
106 }
107 else
108 {
109 SPNumber = osvi->wServicePackMajor;
110 }
111 return SPNumber;
112 }
113
114 INT
115 GetBuildNumber(BOOL bLocal, LPOSVERSIONINFOEX osvi, LPSERVER_INFO_102 pBuf102)
116 {
117 INT BuildNum = 255;
118 if(!bLocal)
119 {
120 /* FIXME: Use Registry to get value */
121 }
122 else
123 {
124 BuildNum = osvi->dwBuildNumber;
125 }
126 return BuildNum;
127 }
128
129 INT GetType(BOOL bLocal, LPOSVERSIONINFOEX osvi, LPSERVER_INFO_102 pBuf102)
130 {
131 if(bLocal)
132 {
133 if(osvi->dwMajorVersion == 5)
134 {
135 if(osvi->dwMinorVersion == 1)
136 {
137 if(osvi->wSuiteMask & VER_SUITE_PERSONAL)
138 return 1;
139 else
140 return 2;
141 }
142 else if(osvi->dwMinorVersion == 2)
143 {
144 if(osvi->wSuiteMask & VER_SUITE_BLADE)
145 return 6;
146 else if(osvi->wSuiteMask & VER_SUITE_DATACENTER)
147 return 5;
148 else if(osvi->wSuiteMask & VER_SUITE_ENTERPRISE)
149 return 4;
150 else
151 return 3;
152 }
153 }
154 }
155 else
156 {
157 /* FIXME: Get this value from registry */
158 }
159 return 255;
160 }
161
162 VOID
163 GetBasicInfo(LPOSVERSIONINFOEX osvi, TCHAR * HostName, TCHAR * OSName, TCHAR * Version, TCHAR * Role, TCHAR * Components)
164 {
165 /* Host Name - COMPUTERNAME*/
166 DWORD bufCharCount = 1024;
167 GetComputerName(HostName, &bufCharCount);
168
169
170 /* OSName - Windows XP Home Editition */
171 if(osvi->dwMajorVersion == 4)
172 {
173 _tcscpy(OSName, _T("Microsoft Windows NT 4.0 "));
174 }
175 else if(osvi->dwMajorVersion == 5)
176 {
177 if(osvi->dwMajorVersion == 0)
178 {
179 _tcscpy(OSName, _T("Microsoft Windows 2000 "));
180 }
181 else if(osvi->dwMinorVersion == 1)
182 {
183 _tcscpy(OSName, _T("Microsoft Windows XP "));
184 }
185 else if(osvi->dwMinorVersion == 2)
186 {
187 _tcscpy(OSName, _T("Microsoft Windows Server 2003 "));
188 }
189 }
190 else if(osvi->dwMajorVersion == 6)
191 {
192 _tcscpy(OSName, _T("Microsoft Windows Vista "));
193 }
194 else
195 {
196 _tcscpy(OSName, _T("Microsoft Windows "));
197 }
198
199 if(osvi->wSuiteMask & VER_SUITE_BLADE)
200 _tcscat(OSName, _T("Web Edition"));
201 if(osvi->wSuiteMask & VER_SUITE_DATACENTER)
202 _tcscat(OSName, _T("Datacenter"));
203 if(osvi->wSuiteMask & VER_SUITE_ENTERPRISE)
204 _tcscat(OSName, _T("Enterprise"));
205 if(osvi->wSuiteMask & VER_SUITE_EMBEDDEDNT)
206 _tcscat(OSName, _T("Embedded"));
207 if(osvi->wSuiteMask & VER_SUITE_PERSONAL)
208 _tcscat(OSName, _T("Home Edition"));
209 if(osvi->wSuiteMask & VER_SUITE_SMALLBUSINESS_RESTRICTED && osvi->wSuiteMask & VER_SUITE_SMALLBUSINESS)
210 _tcscat(OSName, _T("Small Bussiness Edition"));
211
212 /* Version - 5.1 Build 2600 Serivce Pack 2 */
213 _stprintf(Version, _T("%d.%d Build %d %s"),(int)osvi->dwMajorVersion,(int)osvi->dwMinorVersion,(int)osvi->dwBuildNumber, osvi->szCSDVersion);
214
215 /* Role - Workgroup / Server / Domain Controller */
216 if(osvi->wProductType == VER_NT_DOMAIN_CONTROLLER)
217 _tcscpy(Role, _T("Domain Controller"));
218 else if(osvi->wProductType == VER_NT_SERVER)
219 _tcscpy(Role, _T("Server"));
220 else if(osvi->wProductType == VER_NT_WORKSTATION)
221 _tcscpy(Role, _T("Workgroup"));
222
223 /* Components - FIXME: what is something that might be installed? */
224 _tcscat(Components, _T("Not Installed"));
225
226 }
227
228 INT
229 _tmain (VOID)
230 {
231 DWORD Operations = 0;
232 INT ret = 255;
233 INT i = 0;
234 INT argc = 0;
235 /* True if the target is local host */
236 BOOL bLocal = TRUE;
237 DWORD nStatus = 0;
238 TCHAR ServerName[32];
239 TCHAR RemoteResource[32];
240 TCHAR UserName[32] = _T("");
241 TCHAR Password[32] = _T("");
242 LPOSVERSIONINFOEX osvi = NULL;
243 LPSERVER_INFO_102 pBuf102 = NULL;
244 LPTSTR * argv;
245 osvi = (LPOSVERSIONINFOEX)malloc(sizeof(LPOSVERSIONINFOEX));
246 pBuf102 = (LPSERVER_INFO_102)malloc(sizeof(LPSERVER_INFO_102));
247
248 /* Get the command line correctly since it is unicode */
249 argv = CommandLineToArgvW(GetCommandLineW(), &argc);
250
251
252 /* Process flags */
253 if(argc)
254 {
255 for (i = 1; i < argc; i++)
256 {
257 if(!_tcsicmp(argv[i], _T("/ROLE")) && !Operations)
258 Operations |= GETTYPE_ROLE;
259 else if(!_tcsicmp(argv[i], _T("/VER")) && !Operations)
260 Operations |= GETTYPE_VER;
261 else if(!_tcsicmp(argv[i], _T("/MAJV")) && !Operations)
262 Operations |= GETTYPE_MAJV;
263 else if(!_tcsicmp(argv[i], _T("/MINV")) && !Operations)
264 Operations |= GETTYPE_MINV;
265 else if(!_tcsicmp(argv[i], _T("/SP")) && !Operations)
266 Operations |= GETTYPE_SP;
267 else if(!_tcsicmp(argv[i], _T("/BUILD")) && !Operations)
268 Operations |= GETTYPE_BUILD;
269 else if(!_tcsicmp(argv[i], _T("/TYPE")) && !Operations)
270 Operations |= GETTYPE_TYPE;
271 else if(!_tcsicmp(argv[i], _T("/?")) && !Operations)
272 Operations |= GETTYPE_HELP;
273 else if(!_tcsicmp(argv[i], _T("/S")) && i + 1 < argc)
274 {
275 _tcscpy(ServerName,argv[++i]);
276 bLocal = FALSE;
277 }
278 else if(!_wcsicmp(argv[i], L"/U") && i + 1 < argc)
279 _tcscpy(UserName,argv[++i]);
280 else if(!_wcsicmp(argv[i], L"/P") && i + 1 < argc)
281 _tcscpy(Password,argv[++i]);
282 else
283 {
284 wprintf(L"Error in paramters, please see usage\n");
285 return 255;
286 }
287 }
288 }
289
290 /* Some debug info */
291 //_tprintf(_T("%s - %s - %s - %d"), ServerName, UserName, Password, (int)Operations);
292
293 if(!bLocal)
294 {
295 NETRESOURCE nr;
296
297
298 /* \\*IP or Computer Name*\*Share* */
299 _stprintf(RemoteResource, _T("\\\\%s\\IPC$"), ServerName);
300
301 nr.dwType = RESOURCETYPE_ANY;
302 nr.lpLocalName = NULL;
303 nr.lpProvider= NULL;
304 nr.lpRemoteName = RemoteResource;
305
306 /* open a connection to the server with difference user/pass. */
307 nStatus = WNetAddConnection2(&nr, UserName[0]?UserName:NULL,Password[0]?Password:NULL, CONNECT_INTERACTIVE | CONNECT_COMMANDLINE);
308
309 if(nStatus != NO_ERROR)
310 {
311 _tprintf(_T("Error:%d-%d\n"),(int)nStatus,GetLastError());
312 return 255;
313 }
314 }
315
316 /* Use GetVersionEx for anything that we are looking for locally */
317 if(bLocal)
318 {
319 osvi->dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
320 if(!GetVersionEx((LPOSVERSIONINFO)osvi))
321 {
322 _tprintf(_T("Failed to get local information\n"));
323 return 255;
324 }
325 }
326 else
327 {
328 nStatus = NetServerGetInfo(NULL,102,(LPBYTE *)&pBuf102);
329 if (nStatus != NERR_Success)
330 {
331 _tprintf(_T("Failed to connection to remote machine\n"));
332 return 255;
333 }
334
335 }
336
337 if(Operations & GETTYPE_VER)
338 {
339 ret = GetVersionNumber(bLocal, osvi, pBuf102);
340 }
341 else if(Operations & GETTYPE_MAJV)
342 {
343 ret = GetMajValue(TRUE, bLocal, osvi, pBuf102);
344 }
345 else if(Operations & GETTYPE_MINV)
346 {
347 ret = GetMajValue(FALSE, bLocal, osvi, pBuf102);
348 }
349 else if(Operations & GETTYPE_ROLE)
350 {
351 ret = GetSystemRole(bLocal, osvi, pBuf102);
352 }
353 else if(Operations & GETTYPE_SP)
354 {
355 ret = GetServicePack(bLocal, osvi, pBuf102, ServerName);
356 }
357 else if(Operations & GETTYPE_BUILD)
358 {
359 ret = GetBuildNumber(bLocal, osvi, pBuf102);
360 }
361 else if(Operations & GETTYPE_TYPE)
362 {
363 ret = GetType(bLocal, osvi, pBuf102);
364 }
365 else if(Operations & GETTYPE_HELP)
366 {
367 wprintf(L"GETTYPE [/ROLE | /SP | /VER | /MAJV | /MINV | /TYPE | /BUILD]");
368 ret = 0;
369 }
370 else if(!Operations && bLocal)
371 {
372 /* FIXME: what happens when no flags except remote machine, does it
373 it print this info for the remote server? */
374 TCHAR HostName[1024] = _T("");
375 TCHAR OSName[1024] = _T("");
376 TCHAR VersionInfo[1024] = _T("");
377 TCHAR Role[1024] = _T("");
378 TCHAR Components[1024] = _T("");
379 GetBasicInfo(osvi, HostName, OSName, VersionInfo, Role, Components);
380 _tprintf(_T("\nHostname: %s\nName: %s\nVersion:%s\n") ,HostName, OSName, VersionInfo);
381 _tprintf(_T("Role: %s\nComponent: %s\n"), Role, Components);
382 ret = 0;
383 }
384
385 /* Clean up some stuff that that was opened */
386 if(pBuf102)
387 NetApiBufferFree(pBuf102);
388 LocalFree(argv);
389 if(!bLocal)
390 {
391 WNetCancelConnection2(RemoteResource,0,TRUE);
392 }
393 return ret;
394 }