1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * ReactOS Win32 Applications
 * Copyright (C) 2005 ReactOS Team
 *
 * COPYRIGHT:   See COPYING in the top level directory
 * PROJECT:     ReactOS arp utility
 * FILE:        apps/utils/gettype/gettype.c
 * PURPOSE:
 * PROGRAMMERS: Brandon Turner (turnerb7@msu.edu)
 * REVISIONS:
 *   GM 30/10/05 Created
 *
 * FIXME: gettype only supports local computer.
 */
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <lm.h>
#include <shellapi.h>

enum
{
	GETTYPE_ROLE	= 0x001,
	GETTYPE_HELP	= 0x002,
	GETTYPE_SP		= 0x004,
	GETTYPE_VER		= 0x008,
	GETTYPE_MINV	= 0x010,
	GETTYPE_MAJV	= 0x020,
	GETTYPE_TYPE	= 0x040,
	GETTYPE_BUILD	= 0x080
};

INT
GetVersionNumber(BOOL bLocal, LPOSVERSIONINFOEX osvi, LPSERVER_INFO_102 pBuf102)
{
	INT VersionNumber = 255;
	if(pBuf102 != NULL && !bLocal)
	{
		VersionNumber = pBuf102->sv102_version_major * 1000;
		VersionNumber += (pBuf102->sv102_version_minor * 100);
	}
	else if(bLocal)
	{
		VersionNumber = osvi->dwMajorVersion * 1000;
		VersionNumber += (osvi->dwMinorVersion * 100);
	}

	return VersionNumber;
}

INT
GetMajValue(BOOL Major, BOOL bLocal, LPOSVERSIONINFOEX osvi, LPSERVER_INFO_102 pBuf102)
{
	INT VersionNumber = 255;
	if(pBuf102 != NULL && !bLocal)
	{
		if(Major)
			VersionNumber = pBuf102->sv102_version_major * 1000;
		else
			VersionNumber = pBuf102->sv102_version_minor * 100;
	}
	else
	{
		if(Major)
			VersionNumber = osvi->dwMajorVersion * 1000;
		else
			VersionNumber = osvi->dwMinorVersion * 100;
	}
	return VersionNumber;
}

INT
GetSystemRole(BOOL bLocal, LPOSVERSIONINFOEX osvi, LPSERVER_INFO_102 pBuf102)
{

	if(pBuf102 != NULL && !bLocal)
	{
		if ((pBuf102->sv102_type & SV_TYPE_DOMAIN_CTRL) ||
			(pBuf102->sv102_type & SV_TYPE_DOMAIN_BAKCTRL))
			return 1;
		else if(pBuf102->sv102_type & SV_TYPE_SERVER_NT)
			return 2;
		else
			return 3;
	}
	else
	{
		if(osvi->wProductType == VER_NT_DOMAIN_CONTROLLER)
			return 1;
		else if(osvi->wProductType == VER_NT_SERVER)
			return 2;
		else if(osvi->wProductType == VER_NT_WORKSTATION)
			return 3;
	}
	return 255;

}

INT
GetServicePack(BOOL bLocal, LPOSVERSIONINFOEX osvi, LPSERVER_INFO_102 pBuf102, TCHAR * Server)
{
	INT SPNumber = 255;
	if(!bLocal)
	{
		/* FIXME: Use Registry to get value */
	}
	else
	{
		SPNumber = osvi->wServicePackMajor;
	}
	return SPNumber;
}

INT
GetBuildNumber(BOOL bLocal, LPOSVERSIONINFOEX osvi, LPSERVER_INFO_102 pBuf102)
{
	INT BuildNum = 255;
	if(!bLocal)
	{
		/* FIXME: Use Registry to get value */
	}
	else
	{
		BuildNum = osvi->dwBuildNumber;
	}
	return BuildNum;
}

INT GetType(BOOL bLocal, LPOSVERSIONINFOEX osvi, LPSERVER_INFO_102 pBuf102)
{
	if(bLocal)
	{
		if(osvi->dwMajorVersion == 5)
		{
			if(osvi->dwMinorVersion == 1)
			{
				if(osvi->wSuiteMask & VER_SUITE_PERSONAL)
					return 1;
				else
					return 2;
			}
			else if(osvi->dwMinorVersion == 2)
			{
				if(osvi->wSuiteMask & VER_SUITE_BLADE)
					return 6;
				else if(osvi->wSuiteMask & VER_SUITE_DATACENTER)
					return 5;
				else if(osvi->wSuiteMask & VER_SUITE_ENTERPRISE)
					return 4;
				else
					return 3;
			}
		}
	}
	else
	{
		/* FIXME: Get this value from registry */
	}
	return 255;
}

VOID
GetBasicInfo(LPOSVERSIONINFOEX osvi, TCHAR * HostName, TCHAR * OSName, TCHAR * Version, TCHAR * Role, TCHAR * Components)
{
	/* Host Name - COMPUTERNAME*/
	DWORD  bufCharCount = 1024;
	GetComputerName(HostName, &bufCharCount);


	/* OSName - Windows XP Home Editition */
	if(osvi->dwMajorVersion == 4)
	{
		_tcscpy(OSName, _T("Microsoft Windows NT 4.0 "));
	}
	else if(osvi->dwMajorVersion == 5)
	{
		if(osvi->dwMinorVersion == 0)
		{
			_tcscpy(OSName, _T("Microsoft Windows 2000 "));
		}
		else if(osvi->dwMinorVersion == 1)
		{
			_tcscpy(OSName, _T("Microsoft Windows XP "));
		}
		else if(osvi->dwMinorVersion == 2)
		{
			_tcscpy(OSName, _T("Microsoft Windows Server 2003 "));
		}
	}
	else if(osvi->dwMajorVersion == 6)
	{
		_tcscpy(OSName, _T("Microsoft Windows Vista "));
	}
	else
	{
		_tcscpy(OSName, _T("Microsoft Windows "));
	}

	if(osvi->wSuiteMask & VER_SUITE_BLADE)
		_tcscat(OSName, _T("Web Edition"));
	if(osvi->wSuiteMask & VER_SUITE_DATACENTER)
		_tcscat(OSName, _T("Datacenter"));
	if(osvi->wSuiteMask & VER_SUITE_ENTERPRISE)
		_tcscat(OSName, _T("Enterprise"));
	if(osvi->wSuiteMask & VER_SUITE_EMBEDDEDNT)
		_tcscat(OSName, _T("Embedded"));
	if(osvi->wSuiteMask & VER_SUITE_PERSONAL)
		_tcscat(OSName, _T("Home Edition"));
	if(osvi->wSuiteMask & VER_SUITE_SMALLBUSINESS_RESTRICTED && osvi->wSuiteMask & VER_SUITE_SMALLBUSINESS)
		_tcscat(OSName, _T("Small Bussiness Edition"));

	/* Version - 5.1 Build 2600 Serivce Pack 2 */
	_stprintf(Version, _T("%d.%d Build %d %s"),(int)osvi->dwMajorVersion,(int)osvi->dwMinorVersion,(int)osvi->dwBuildNumber, osvi->szCSDVersion);

	/* Role - Workgroup / Server / Domain Controller */
	if(osvi->wProductType == VER_NT_DOMAIN_CONTROLLER)
		_tcscpy(Role, _T("Domain Controller"));
	else if(osvi->wProductType == VER_NT_SERVER)
		_tcscpy(Role, _T("Server"));
	else if(osvi->wProductType == VER_NT_WORKSTATION)
		_tcscpy(Role, _T("Workgroup"));

	/* Components - FIXME: what is something that might be installed? */
	_tcscat(Components, _T("Not Installed"));

}

INT
_tmain (VOID)
{
	DWORD Operations = 0;
	INT ret = 255;
	INT i = 0;
	INT argc = 0;
	/* True if the target is local host */
	BOOL bLocal = TRUE;
	DWORD nStatus = 0;
	TCHAR ServerName[32];
	TCHAR RemoteResource[32];
	TCHAR UserName[32] = _T("");
	TCHAR Password[32] = _T("");
	LPOSVERSIONINFOEX osvi = NULL;
	LPSERVER_INFO_102 pBuf102 = NULL;
	LPTSTR * argv;
	osvi = (LPOSVERSIONINFOEX)malloc(sizeof(LPOSVERSIONINFOEX));
	pBuf102 = (LPSERVER_INFO_102)malloc(sizeof(LPSERVER_INFO_102));

	/* Get the command line correctly since it is unicode */
	argv = CommandLineToArgvW(GetCommandLineW(), &argc);


	/* Process flags */
	if(argc)
	{
		for (i = 1; i < argc; i++)
		{
			if(!_tcsicmp(argv[i], _T("/ROLE")) && !Operations)
				Operations |= GETTYPE_ROLE;
			else if(!_tcsicmp(argv[i], _T("/VER")) && !Operations)
				Operations |= GETTYPE_VER;
			else if(!_tcsicmp(argv[i], _T("/MAJV")) && !Operations)
				Operations |= GETTYPE_MAJV;
			else if(!_tcsicmp(argv[i], _T("/MINV")) && !Operations)
				Operations |= GETTYPE_MINV;
			else if(!_tcsicmp(argv[i], _T("/SP")) && !Operations)
				Operations |= GETTYPE_SP;
			else if(!_tcsicmp(argv[i], _T("/BUILD")) && !Operations)
				Operations |= GETTYPE_BUILD;
			else if(!_tcsicmp(argv[i], _T("/TYPE")) && !Operations)
				Operations |= GETTYPE_TYPE;
			else if(!_tcsicmp(argv[i], _T("/?")) && !Operations)
				Operations |= GETTYPE_HELP;
			else if(!_tcsicmp(argv[i], _T("/S")) && i + 1 < argc)
			{
				_tcscpy(ServerName,argv[++i]);
				bLocal = FALSE;
			}
			else if(!_wcsicmp(argv[i], L"/U") && i + 1 < argc)
				_tcscpy(UserName,argv[++i]);
			else if(!_wcsicmp(argv[i], L"/P") && i + 1 < argc)
				_tcscpy(Password,argv[++i]);
			else
			{
				wprintf(L"Error in paramters, please see usage\n");
				return 255;
			}
		}
	}

	/* Some debug info */
	//_tprintf(_T("%s - %s - %s - %d"), ServerName, UserName, Password, (int)Operations);

	if(!bLocal)
	{
		NETRESOURCE nr;


		/* \\*IP or Computer Name*\*Share* */
		_stprintf(RemoteResource, _T("\\\\%s\\IPC$"), ServerName);

		nr.dwType = RESOURCETYPE_ANY;
		nr.lpLocalName = NULL;
		nr.lpProvider= NULL;
		nr.lpRemoteName	= RemoteResource;

		/* open a connection to the server with difference user/pass. */
		nStatus = WNetAddConnection2(&nr, UserName[0]?UserName:NULL,Password[0]?Password:NULL, CONNECT_INTERACTIVE | CONNECT_COMMANDLINE);

		if(nStatus != NO_ERROR)
		{
			_tprintf(_T("Error:%d-%d\n"),(int)nStatus,GetLastError());
			return 255;<--- Memory leak: osvi<--- Memory leak: pBuf102
		}
	}

	/* Use GetVersionEx for anything that we are looking for locally */
	if(bLocal)
	{
		osvi->dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
		if(!GetVersionEx((LPOSVERSIONINFO)osvi))
		{
			_tprintf(_T("Failed to get local information\n"));
			return 255;<--- Memory leak: pBuf102
		}
	}
	else
	{
		nStatus = NetServerGetInfo(NULL,102,(LPBYTE *)&pBuf102);
		if (nStatus != NERR_Success)
		{
			_tprintf(_T("Failed to connection to remote machine\n"));
			return 255;<--- Memory leak: osvi
		}

	}

	if(Operations & GETTYPE_VER)
	{
		ret = GetVersionNumber(bLocal, osvi, pBuf102);
	}
	else if(Operations & GETTYPE_MAJV)
	{
		ret = GetMajValue(TRUE, bLocal, osvi, pBuf102);
	}
	else if(Operations & GETTYPE_MINV)
	{
		ret = GetMajValue(FALSE, bLocal, osvi, pBuf102);
	}
	else if(Operations & GETTYPE_ROLE)
	{
		ret = GetSystemRole(bLocal, osvi, pBuf102);
	}
	else if(Operations & GETTYPE_SP)
	{
		ret = GetServicePack(bLocal, osvi, pBuf102, ServerName);
	}
	else if(Operations & GETTYPE_BUILD)
	{
		ret = GetBuildNumber(bLocal, osvi, pBuf102);
	}
	else if(Operations & GETTYPE_TYPE)
	{
		ret = GetType(bLocal, osvi, pBuf102);
	}
	else if(Operations & GETTYPE_HELP)
	{
		wprintf(L"GETTYPE  [/ROLE | /SP | /VER | /MAJV | /MINV | /TYPE | /BUILD]");
		ret = 0;
	}
	else if(!Operations && bLocal)
	{
		/* FIXME: what happens when no flags except remote machine, does it
		it print this info for the remote server? */
		TCHAR HostName[1024] = _T("");
		TCHAR OSName[1024] = _T("");
		TCHAR VersionInfo[1024] = _T("");
		TCHAR Role[1024] = _T("");
		TCHAR Components[1024] = _T("");
		GetBasicInfo(osvi, HostName, OSName, VersionInfo, Role, Components);
		_tprintf(_T("\nHostname: %s\nName: %s\nVersion:%s\n") ,HostName, OSName, VersionInfo);
		_tprintf(_T("Role: %s\nComponent: %s\n"), Role, Components);
		ret = 0;
	}

	/* Clean up some stuff that that was opened */
	if(pBuf102)
		NetApiBufferFree(pBuf102);
	LocalFree(argv);
	if(!bLocal)
	{
		WNetCancelConnection2(RemoteResource,0,TRUE);
	}
	return ret;
}