Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

630 lines
16 KiB

  1. /*++
  2. Filename : servers.c
  3. Description: This file will be for testing the servers access.
  4. Created by: Wally Ho
  5. History: Created on 03/29/99.
  6. Contains these functions:
  7. 1. IsServerOnline (IN LPTSTR szMachineName)
  8. 2. ServerOnlineThread (IN LPTSTR szServerFile)
  9. --*/
  10. #include "setuplogEXE.h"
  11. BOOL
  12. IsServerOnline(IN LPTSTR szMachineName, IN LPTSTR szSpecifyShare)
  13. /*++
  14. Routine Description:
  15. This will go through the list of servers specified in setuplogEXE.h
  16. It will return the first in it sees and reset the global server share
  17. name.
  18. Arguments:
  19. The machineName (Filename with build etc) so the test file will get overwritten.
  20. Manual Server Name: NULL will give default behaviour.
  21. Return Value:
  22. TRUE for success.
  23. FALSE for no name.
  24. --*/
  25. {
  26. DWORD dw;
  27. HANDLE hThrd;
  28. INT i;
  29. TCHAR szServerFile[ MAX_PATH ];
  30. DWORD dwTimeOutInterval;
  31. i = 0;
  32. //
  33. // This should allow for a
  34. // manually specified server.
  35. //
  36. if (NULL != szSpecifyShare){
  37. _tcscpy(g_szServerShare,szSpecifyShare);
  38. return TRUE;
  39. }
  40. //
  41. // Initialize the Server.
  42. // Variable. Since we are using a single thread
  43. // to do a time out we don't care about mutexes and
  44. // sychronization.
  45. //
  46. g_bServerOnline = FALSE;
  47. while ( i < NUM_SERVERS){
  48. _stprintf (szServerFile, TEXT("%s\\%s"),s[i].szSvr,szMachineName );
  49. //
  50. // Spawn the thread
  51. //
  52. hThrd = CreateThread(NULL,
  53. 0,
  54. (LPTHREAD_START_ROUTINE) ServerOnlineThread,
  55. (LPTSTR) szServerFile,
  56. 0,
  57. &dw);
  58. //
  59. // This is in milli seconds so the time out is secs.
  60. //
  61. dwTimeOutInterval = TIME_TIMEOUT * 1000;
  62. s[i].dwTimeOut = WaitForSingleObject (hThrd, dwTimeOutInterval);
  63. CloseHandle (hThrd);
  64. //
  65. // This means the server passed the timeout.
  66. //
  67. if (s[i].dwTimeOut != WAIT_TIMEOUT &&
  68. g_bServerOnline == TRUE){
  69. //
  70. // Copy the Share to the glowbal var.
  71. //
  72. _tcscpy(g_szServerShare,s[i].szSvr);
  73. return TRUE;
  74. }
  75. i++;
  76. }
  77. return FALSE;
  78. }
  79. BOOL
  80. ServerOnlineThread(IN LPTSTR szServerFile)
  81. /*++
  82. Routine Description:
  83. This create a thread and then time it out to see if we can get to
  84. a server faster.
  85. Arguments:
  86. The machineName so the test file will get overwritten.
  87. Return Value:
  88. --*/
  89. {
  90. BOOL bCopy = FALSE;
  91. TCHAR szFileSrc [MAX_PATH];
  92. TCHAR szServerTestFile [MAX_PATH];
  93. //
  94. // Use this to get the location
  95. // setuplog.exe is run from. this tool
  96. //
  97. GetModuleFileName (NULL, szFileSrc, MAX_PATH);
  98. //
  99. // Make a unique test file.
  100. //
  101. _stprintf(szServerTestFile,TEXT("%s.SERVERTEST"),szServerFile);
  102. bCopy = CopyFile( szFileSrc,szServerTestFile, FALSE);
  103. if (bCopy != FALSE){
  104. //
  105. // If Succeeded Delete the test file.
  106. //
  107. DeleteFile(szServerTestFile);
  108. g_bServerOnline = TRUE;
  109. return TRUE;
  110. }
  111. else{
  112. g_bServerOnline = FALSE;
  113. return FALSE;
  114. }
  115. }
  116. /*
  117. INT i;
  118. NETRESOURCE NetResource ;
  119. i = 0;
  120. while ( i < NUM_SERVERS){
  121. //
  122. // Prep the struct.
  123. //
  124. ZeroMemory( &NetResource, sizeof( NetResource ) );
  125. NetResource.dwType = RESOURCETYPE_DISK ;
  126. NetResource.lpLocalName = "" ;
  127. NetResource.lpRemoteName = s[i].szSvr;
  128. NetResource.lpProvider = "" ;
  129. //
  130. // Try with default password and user.
  131. // This should work as its open to everyone.
  132. //
  133. s[i].dwNetStatus = WNetAddConnection2( &NetResource,NULL,NULL, 0 );
  134. //
  135. // Try default PW / USERID from setuplog.h
  136. //
  137. if (s[i].dwNetStatus != 0)
  138. s[i].dwNetStatus = WNetAddConnection2( &NetResource,LOGSHARE_PW,LOGSHARE_USER,0 );
  139. WNetCancelConnection2( g_szServerShare, 0, TRUE );
  140. if (s[i].dwNetStatus == NO_ERROR){
  141. //
  142. // Copy the Share to the glowbal var.
  143. //
  144. _tcscpy(g_szServerShare,s[i].szSvr);
  145. return TRUE;
  146. }
  147. i++;
  148. }
  149. //
  150. // No Valid name.
  151. // Return false so we won't write.
  152. return FALSE;
  153. */
  154. BOOL IsMSI(VOID)
  155. /*++
  156. Routine Description:
  157. This will check if its an MSI install.
  158. It will check for the running process
  159. and then check for the path.
  160. Arguments:
  161. Return Value:
  162. BOOL - True if link is good. False otherwise.
  163. --*/
  164. {
  165. DWORD numTasks = 0;
  166. TASK_LIST tlist[ MAX_PATH ];
  167. UINT i;
  168. BOOL bFound = FALSE;
  169. //
  170. // Get the Running Tasks.
  171. //
  172. numTasks = GetTaskList(tlist, MAX_PATH);
  173. //
  174. // If the MSI process exists log it as such.
  175. //
  176. for(i = 1; i <= numTasks; i++){
  177. if(_tcsstr(tlist[i].ProcessName, TEXT("msiexec.exe"))){
  178. MessageBox(NULL,tlist[i].ProcessName, TEXT("Caption"),MB_OK);
  179. lpCmdFrom.b_MsiInstall = TRUE;
  180. return FALSE;
  181. }else{
  182. lpCmdFrom.b_MsiInstall = TRUE;
  183. return TRUE;
  184. }
  185. }
  186. return TRUE;
  187. }
  188. DWORD
  189. GetTaskList(
  190. PTASK_LIST pTask,
  191. DWORD dwNumTasks
  192. )
  193. /*++
  194. // Borrowed with modifications from tlist a wesw invention.
  195. Routine Description:
  196. Provides an API for getting a list of tasks running at the time of the
  197. API call. This function uses the registry performance data to get the
  198. task list and is therefor straight WIN32 calls that anyone can call.
  199. Arguments:
  200. dwNumTasks - maximum number of tasks that the pTask array can hold
  201. Return Value:
  202. Number of tasks placed into the pTask array.
  203. --*/
  204. {
  205. DWORD rc;
  206. HKEY hKeyNames;
  207. DWORD dwType;
  208. DWORD dwSize;
  209. LPBYTE buf = NULL;
  210. CHAR szSubKey[1024];
  211. LANGID lid;
  212. LPSTR p;
  213. LPSTR p2;
  214. PPERF_DATA_BLOCK pPerf;
  215. PPERF_OBJECT_TYPE pObj;
  216. PPERF_INSTANCE_DEFINITION pInst;
  217. PPERF_COUNTER_BLOCK pCounter;
  218. PPERF_COUNTER_DEFINITION pCounterDef;
  219. DWORD i;
  220. DWORD dwProcessIdTitle;
  221. DWORD dwProcessIdCounter;
  222. CHAR szProcessName[MAX_PATH];
  223. DWORD dwLimit = dwNumTasks - 1;
  224. //
  225. // Look for the list of counters. Always use the neutral
  226. // English version, regardless of the local language. We
  227. // are looking for some particular keys, and we are always
  228. // going to do our looking in English. We are not going
  229. // to show the user the counter names, so there is no need
  230. // to go find the corresponding name in the local language.
  231. //
  232. lid = MAKELANGID( LANG_ENGLISH, SUBLANG_NEUTRAL );
  233. sprintf( szSubKey, "%s\\%03x", REGKEY_PERF, lid );
  234. rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  235. szSubKey,
  236. 0,
  237. KEY_READ,
  238. &hKeyNames
  239. );
  240. if (rc != ERROR_SUCCESS) {
  241. goto exit;
  242. }
  243. //
  244. // get the buffer size for the counter names
  245. //
  246. rc = RegQueryValueEx( hKeyNames,
  247. REGSUBKEY_COUNTERS,
  248. NULL,
  249. &dwType,
  250. NULL,
  251. &dwSize
  252. );
  253. if (rc != ERROR_SUCCESS) {
  254. goto exit;
  255. }
  256. //
  257. // allocate the counter names buffer
  258. //
  259. buf = (LPBYTE) malloc( dwSize );
  260. if (buf == NULL) {
  261. goto exit;
  262. }
  263. memset( buf, 0, dwSize );
  264. //
  265. // read the counter names from the registry
  266. //
  267. rc = RegQueryValueEx( hKeyNames,
  268. REGSUBKEY_COUNTERS,
  269. NULL,
  270. &dwType,
  271. buf,
  272. &dwSize
  273. );
  274. if (rc != ERROR_SUCCESS) {
  275. goto exit;
  276. }
  277. //
  278. // now loop thru the counter names looking for the following counters:
  279. //
  280. // 1. "Process" process name
  281. // 2. "ID Process" process id
  282. //
  283. // the buffer contains multiple null terminated strings and then
  284. // finally null terminated at the end. the strings are in pairs of
  285. // counter number and counter name.
  286. //
  287. p = buf;
  288. while (*p) {
  289. if (p > buf) {
  290. for( p2=p-2; isdigit(*p2); p2--) ;
  291. }
  292. if (_stricmp(p, PROCESS_COUNTER) == 0) {
  293. //
  294. // look backwards for the counter number
  295. //
  296. for( p2=p-2; isdigit(*p2); p2--) ;
  297. strcpy( szSubKey, p2+1 );
  298. }
  299. else
  300. if (_stricmp(p, PROCESSID_COUNTER) == 0) {
  301. //
  302. // look backwards for the counter number
  303. //
  304. for( p2=p-2; isdigit(*p2); p2--) ;
  305. dwProcessIdTitle = atol( p2+1 );
  306. }
  307. //
  308. // next string
  309. //
  310. p += (strlen(p) + 1);
  311. }
  312. //
  313. // free the counter names buffer
  314. //
  315. free( buf );
  316. //
  317. // allocate the initial buffer for the performance data
  318. //
  319. dwSize = INITIAL_SIZE;
  320. buf = malloc( dwSize );
  321. if (buf == NULL) {
  322. goto exit;
  323. }
  324. memset( buf, 0, dwSize );
  325. while (TRUE) {
  326. rc = RegQueryValueEx( HKEY_PERFORMANCE_DATA,
  327. szSubKey,
  328. NULL,
  329. &dwType,
  330. buf,
  331. &dwSize
  332. );
  333. pPerf = (PPERF_DATA_BLOCK) buf;
  334. //
  335. // check for success and valid perf data block signature
  336. //
  337. if ((rc == ERROR_SUCCESS) &&
  338. (dwSize > 0) &&
  339. (pPerf)->Signature[0] == (WCHAR)'P' &&
  340. (pPerf)->Signature[1] == (WCHAR)'E' &&
  341. (pPerf)->Signature[2] == (WCHAR)'R' &&
  342. (pPerf)->Signature[3] == (WCHAR)'F' ) {
  343. break;
  344. }
  345. //
  346. // if buffer is not big enough, reallocate and try again
  347. //
  348. if (rc == ERROR_MORE_DATA) {
  349. dwSize += EXTEND_SIZE;
  350. buf = realloc( buf, dwSize );
  351. memset( buf, 0, dwSize );
  352. }
  353. else {
  354. goto exit;
  355. }
  356. }
  357. //
  358. // set the perf_object_type pointer
  359. //
  360. pObj = (PPERF_OBJECT_TYPE) ((DWORD*)pPerf + pPerf->HeaderLength);
  361. //
  362. // loop thru the performance counter definition records looking
  363. // for the process id counter and then save its offset
  364. //
  365. pCounterDef = (PPERF_COUNTER_DEFINITION) ((DWORD *)pObj + pObj->HeaderLength);
  366. for (i=0; i<(DWORD)pObj->NumCounters; i++) {
  367. if (pCounterDef->CounterNameTitleIndex == dwProcessIdTitle) {
  368. dwProcessIdCounter = pCounterDef->CounterOffset;
  369. break;
  370. }
  371. pCounterDef++;
  372. }
  373. dwNumTasks = min( dwLimit, (DWORD)pObj->NumInstances );
  374. pInst = (PPERF_INSTANCE_DEFINITION) ((DWORD*)pObj + pObj->DefinitionLength);
  375. //
  376. // loop thru the performance instance data extracting each process name
  377. // and process id
  378. //
  379. for (i=0; i<dwNumTasks; i++) {
  380. //
  381. // pointer to the process name
  382. //
  383. p = (LPSTR) ((DWORD*)pInst + pInst->NameOffset);
  384. //
  385. // convert it to ascii
  386. //
  387. rc = WideCharToMultiByte( CP_ACP,
  388. 0,
  389. (LPCWSTR)p,
  390. -1,
  391. szProcessName,
  392. sizeof(szProcessName),
  393. NULL,
  394. NULL
  395. );
  396. if (!rc) {
  397. //
  398. // if we cant convert the string then use a bogus value
  399. //
  400. strcpy( pTask->ProcessName, UNKNOWN_TASK );
  401. }
  402. if (strlen(szProcessName)+4 <= sizeof(pTask->ProcessName)) {
  403. strcpy( pTask->ProcessName, szProcessName );
  404. strcat( pTask->ProcessName, ".exe" );
  405. }
  406. //
  407. // get the process id
  408. //
  409. pCounter = (PPERF_COUNTER_BLOCK) ((DWORD*)pInst + pInst->ByteLength);
  410. pTask->flags = 0;
  411. pTask->dwProcessId = *((LPDWORD) ((DWORD*)pCounter + dwProcessIdCounter));
  412. if (pTask->dwProcessId == 0) {
  413. pTask->dwProcessId = (DWORD)-2;
  414. }
  415. //
  416. // next process
  417. //
  418. pTask++;
  419. pInst = (PPERF_INSTANCE_DEFINITION) ((DWORD*)pCounter + pCounter->ByteLength);
  420. }
  421. exit:
  422. if (buf) {
  423. free( buf );
  424. }
  425. RegCloseKey( hKeyNames );
  426. RegCloseKey( HKEY_PERFORMANCE_DATA );
  427. //
  428. // W.Ho added a minus 1 to get it to reflect the
  429. // tasks properly.
  430. //
  431. return dwNumTasks -1;
  432. }
  433. /*
  434. typedef struct _SERVERS {
  435. TCHAR szSvr [ MAX_PATH ];
  436. BOOL bCFTest;
  437. DWORD dwNetStatus;
  438. } *LPSERVERS, SERVERS;
  439. typedef struct _ERRMSG {
  440. TCHAR szMsg[ MAX_PATH ];
  441. DWORD dwErr;
  442. } *LPERRMSG, ERRMSG;
  443. BOOL
  444. IsServerOnline(VOID)
  445. /*++
  446. Routine Description:
  447. Arguments:
  448. Return Value:
  449. NONE.
  450. --
  451. {
  452. #define NUM_SERVERS 6
  453. INT i;
  454. TCHAR sz[ MAX_PATH ];
  455. ERRMSG e[12] = {
  456. {TEXT("Access is denied."), ERROR_ACCESS_DENIED},
  457. {TEXT("The device specified in the lpLocalName parameter is already connected."), ERROR_ALREADY_ASSIGNED },
  458. {TEXT("The device type and the resource type do not match."), ERROR_BAD_DEV_TYPE},
  459. {TEXT("The value specified in lpLocalName is invalid."), ERROR_BAD_DEVICE},
  460. {TEXT("The value specified in the lpRemoteName parameter is not valid or cannot be located."), ERROR_BAD_NET_NAME},
  461. {TEXT("The user profile is in an incorrect format."), ERROR_BAD_PROFILE},
  462. {TEXT("The system is unable to open the user profile to process persistent connections."),ERROR_CANNOT_OPEN_PROFILE },
  463. {TEXT("An entry for the device specified in lpLocalName is already in the user profile."), ERROR_DEVICE_ALREADY_REMEMBERED},
  464. {TEXT("A network-specific error occurred. To get a description of the error, use the WNetGetLastError function."), ERROR_EXTENDED_ERROR},
  465. {TEXT("The specified password is invalid."), ERROR_INVALID_PASSWORD},
  466. {TEXT("The operation cannot be performed because either a network component is not started or the specified name cannot be used."),ERROR_NO_NET_OR_BAD_PATH },
  467. {TEXT("The network is not present."),ERROR_NO_NETWORK}
  468. };
  469. SERVERS s[NUM_SERVERS] ={
  470. {TEXT("\\\\donkeykongjr\\public"), -1, -1},
  471. {TEXT("\\\\popcorn\\public"), -1, -1},
  472. {TEXT("\\\\NotExists\\idwlog"), -1, -1},
  473. {TEXT("\\\\Paddy\\idwlog"), -1, -1},
  474. {TEXT("\\\\Bear\\idwlog"), -1, -1},
  475. {TEXT("\\\\JustTesting\\idwlog"), -1, -1}
  476. };
  477. for (i = 0; i < 12; i++) {
  478. _tprintf(TEXT("Error %s %lu\n"),e[i].szMsg, e[i].dwErr);
  479. }
  480. for (i = 0; i < NUM_SERVERS; i++){
  481. s[i].dwNetStatus = WNetAddConnection(TEXT("donkeykongjr\\public\0"),NULL,NULL);
  482. _stprintf(sz,TEXT("%s%s"),s[i].szSvr,TEXT("\\test") );
  483. s[i].bCFTest = CopyFile(TEXT("c:\\test"),sz,FALSE);
  484. _tprintf(TEXT("Did this work for %s %s %lu\n"),
  485. sz,
  486. s[i].bCFTest? TEXT("WORKED"): TEXT("FAILED"),
  487. s[i].dwNetStatus
  488. );
  489. }
  490. return FALSE;
  491. }
  492. */
  493.