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.

1091 lines
32 KiB

  1. /*++
  2. Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. Routing\Netsh\wins\srvrmon.c
  5. Abstract:
  6. WINS Server Command dispatcher.
  7. Created by:
  8. Shubho Bhattacharya(a-sbhatt) on 12/14/98
  9. --*/
  10. #include "precomp.h"
  11. LPWSTR g_ServerNameUnicode = NULL;
  12. LPSTR g_ServerNameAnsi = NULL;
  13. CHAR g_ServerIpAddressAnsiString[MAX_IP_STRING_LEN+1] = {'\0'};
  14. WCHAR g_ServerIpAddressUnicodeString[MAX_IP_STRING_LEN+1] = {L'\0'};
  15. HKEY g_hServerRegKey = NULL;
  16. WCHAR g_ServerNetBiosName[MAX_COMPUTER_NAME_LEN] = {L'\0'};
  17. handle_t g_hBind = NULL;
  18. WINSINTF_BIND_DATA_T g_BindData;
  19. BOOL g_fServer = FALSE;
  20. CMD_ENTRY g_SrvrAddCmdTable[] =
  21. {
  22. CREATE_CMD_ENTRY(SRVR_ADD_NAME, HandleSrvrAddName),
  23. CREATE_CMD_ENTRY(SRVR_ADD_PARTNER, HandleSrvrAddPartner),
  24. CREATE_CMD_ENTRY(SRVR_ADD_PNGSERVER, HandleSrvrAddPersona),
  25. CREATE_CMD_ENTRY(SRVR_ADD_PGSERVER, HandleSrvrAddPersona)
  26. };
  27. CMD_ENTRY g_SrvrCheckCmdTable[] =
  28. {
  29. CREATE_CMD_ENTRY(SRVR_CHECK_DATABASE, HandleSrvrCheckDatabase),
  30. CREATE_CMD_ENTRY(SRVR_CHECK_NAME, HandleSrvrCheckName),
  31. CREATE_CMD_ENTRY(SRVR_CHECK_VERSION, HandleSrvrCheckVersion),
  32. };
  33. CMD_ENTRY g_SrvrDeleteCmdTable[] =
  34. {
  35. CREATE_CMD_ENTRY(SRVR_DELETE_NAME, HandleSrvrDeleteName),
  36. CREATE_CMD_ENTRY(SRVR_DELETE_PARTNER, HandleSrvrDeletePartner),
  37. CREATE_CMD_ENTRY(SRVR_DELETE_RECORDS, HandleSrvrDeleteRecords),
  38. CREATE_CMD_ENTRY(SRVR_DELETE_WINS, HandleSrvrDeleteWins),
  39. CREATE_CMD_ENTRY(SRVR_DELETE_PNGSERVER, HandleSrvrDeletePersona),
  40. CREATE_CMD_ENTRY(SRVR_DELETE_PGSERVER, HandleSrvrDeletePersona)
  41. };
  42. CMD_ENTRY g_SrvrInitCmdTable[] =
  43. {
  44. CREATE_CMD_ENTRY(SRVR_INIT_BACKUP, HandleSrvrInitBackup),
  45. CREATE_CMD_ENTRY(SRVR_INIT_IMPORT, HandleSrvrInitImport),
  46. CREATE_CMD_ENTRY(SRVR_INIT_PULL, HandleSrvrInitPull),
  47. CREATE_CMD_ENTRY(SRVR_INIT_PULLRANGE, HandleSrvrInitPullrange),
  48. CREATE_CMD_ENTRY(SRVR_INIT_PUSH, HandleSrvrInitPush),
  49. CREATE_CMD_ENTRY(SRVR_INIT_REPLICATE, HandleSrvrInitReplicate),
  50. CREATE_CMD_ENTRY(SRVR_INIT_RESTORE, HandleSrvrInitRestore),
  51. CREATE_CMD_ENTRY(SRVR_INIT_SCAVENGE, HandleSrvrInitScavenge),
  52. CREATE_CMD_ENTRY(SRVR_INIT_SEARCH, HandleSrvrInitSearch),
  53. };
  54. CMD_ENTRY g_SrvrResetCmdTable[] =
  55. {
  56. CREATE_CMD_ENTRY(SRVR_RESET_COUNTER, HandleSrvrResetCounter),
  57. };
  58. CMD_ENTRY g_SrvrSetCmdTable[] =
  59. {
  60. CREATE_CMD_ENTRY(SRVR_SET_AUTOPARTNERCONFIG, HandleSrvrSetAutopartnerconfig),
  61. CREATE_CMD_ENTRY(SRVR_SET_BACKUPPATH, HandleSrvrSetBackuppath),
  62. CREATE_CMD_ENTRY(SRVR_SET_BURSTPARAM, HandleSrvrSetBurstparam),
  63. CREATE_CMD_ENTRY(SRVR_SET_DEFAULTPARAM, HandleSrvrSetDefaultparam),
  64. CREATE_CMD_ENTRY(SRVR_SET_LOGPARAM, HandleSrvrSetLogparam),
  65. CREATE_CMD_ENTRY(SRVR_SET_MIGRATEFLAG, HandleSrvrSetMigrateflag),
  66. CREATE_CMD_ENTRY(SRVR_SET_NAMERECORD, HandleSrvrSetNamerecord),
  67. CREATE_CMD_ENTRY(SRVR_SET_PERIODICDBCHECKING, HandleSrvrSetPeriodicdbchecking),
  68. CREATE_CMD_ENTRY(SRVR_SET_PULLPERSISTENTCONNECTION, HandleSrvrSetPullpersistentconnection),
  69. CREATE_CMD_ENTRY(SRVR_SET_PUSHPERSISTENTCONNECTION, HandleSrvrSetPushpersistentconnection),
  70. CREATE_CMD_ENTRY(SRVR_SET_PULLPARAM, HandleSrvrSetPullparam),
  71. CREATE_CMD_ENTRY(SRVR_SET_PUSHPARAM, HandleSrvrSetPushparam),
  72. CREATE_CMD_ENTRY(SRVR_SET_REPLICATEFLAG, HandleSrvrSetReplicateflag),
  73. CREATE_CMD_ENTRY(SRVR_SET_STARTVERSION, HandleSrvrSetStartversion),
  74. CREATE_CMD_ENTRY(SRVR_SET_PGMODE, HandleSrvrSetPersMode)
  75. };
  76. CMD_ENTRY g_SrvrShowCmdTable[] =
  77. {
  78. CREATE_CMD_ENTRY(SRVR_SHOW_DOMAIN, HandleSrvrShowDomain),
  79. CREATE_CMD_ENTRY(SRVR_SHOW_DATABASE, HandleSrvrShowDatabase),
  80. CREATE_CMD_ENTRY(SRVR_SHOW_INFO, HandleSrvrShowInfo),
  81. CREATE_CMD_ENTRY(SRVR_SHOW_NAME, HandleSrvrShowName),
  82. CREATE_CMD_ENTRY(SRVR_SHOW_PARTNER, HandleSrvrShowPartner),
  83. CREATE_CMD_ENTRY(SRVR_SHOW_PARTNERPROPERTIES, HandleSrvrShowPartnerproperties),
  84. CREATE_CMD_ENTRY(SRVR_SHOW_PULLPARTNERPROPERTIES, HandleSrvrShowPullpartnerproperties),
  85. CREATE_CMD_ENTRY(SRVR_SHOW_PUSHPARTNERPROPERTIES, HandleSrvrShowPushpartnerproperties),
  86. CREATE_CMD_ENTRY(SRVR_SHOW_RECCOUNT, HandleSrvrShowReccount),
  87. CREATE_CMD_ENTRY(SRVR_SHOW_RECBYVERSION, HandleSrvrShowRecbyversion),
  88. CREATE_CMD_ENTRY(SRVR_SHOW_SERVER, HandleSrvrShowServer),
  89. CREATE_CMD_ENTRY(SRVR_SHOW_SERVERSTATISTICS, HandleSrvrShowStatistics),
  90. CREATE_CMD_ENTRY(SRVR_SHOW_VERSION, HandleSrvrShowVersion),
  91. CREATE_CMD_ENTRY(SRVR_SHOW_VERSIONMAP, HandleSrvrShowVersionmap),
  92. };
  93. CMD_GROUP_ENTRY g_SrvrCmdGroups[] =
  94. {
  95. CREATE_CMD_GROUP_ENTRY(GROUP_ADD, g_SrvrAddCmdTable),
  96. CREATE_CMD_GROUP_ENTRY(GROUP_CHECK, g_SrvrCheckCmdTable),
  97. CREATE_CMD_GROUP_ENTRY(GROUP_DELETE, g_SrvrDeleteCmdTable),
  98. CREATE_CMD_GROUP_ENTRY(GROUP_INIT, g_SrvrInitCmdTable),
  99. CREATE_CMD_GROUP_ENTRY(GROUP_RESET, g_SrvrResetCmdTable),
  100. CREATE_CMD_GROUP_ENTRY(GROUP_SET, g_SrvrSetCmdTable),
  101. CREATE_CMD_GROUP_ENTRY(GROUP_SHOW, g_SrvrShowCmdTable),
  102. };
  103. CMD_ENTRY g_SrvrCmds[] =
  104. {
  105. CREATE_CMD_ENTRY(WINS_DUMP, HandleSrvrDump),
  106. CREATE_CMD_ENTRY(WINS_HELP1, HandleSrvrHelp),
  107. CREATE_CMD_ENTRY(WINS_HELP2, HandleSrvrHelp),
  108. CREATE_CMD_ENTRY(WINS_HELP3, HandleSrvrHelp),
  109. CREATE_CMD_ENTRY(WINS_HELP4, HandleSrvrHelp),
  110. };
  111. ULONG g_ulSrvrNumTopCmds = sizeof(g_SrvrCmds)/sizeof(CMD_ENTRY);
  112. ULONG g_ulSrvrNumGroups = sizeof(g_SrvrCmdGroups)/sizeof(CMD_GROUP_ENTRY);
  113. DWORD
  114. WINAPI
  115. SrvrCommit(
  116. IN DWORD dwAction
  117. )
  118. {
  119. BOOL bCommit, bFlush = FALSE;
  120. switch(dwAction)
  121. {
  122. case NETSH_COMMIT:
  123. {
  124. if(g_bCommit)
  125. {
  126. return NO_ERROR;
  127. }
  128. g_bCommit = TRUE;
  129. break;
  130. }
  131. case NETSH_UNCOMMIT:
  132. {
  133. g_bCommit = FALSE;
  134. return NO_ERROR;
  135. }
  136. case NETSH_SAVE:
  137. {
  138. if(g_bCommit)
  139. {
  140. return NO_ERROR;
  141. }
  142. break;
  143. }
  144. case NETSH_FLUSH:
  145. {
  146. //
  147. // Action is a flush. Srvr current state is commit, then
  148. // nothing to be done.
  149. //
  150. if(g_bCommit)
  151. {
  152. return NO_ERROR;
  153. }
  154. bFlush = TRUE;
  155. break;
  156. }
  157. default:
  158. {
  159. return NO_ERROR;
  160. }
  161. }
  162. //
  163. // Switched to commit mode. So set all valid info in the
  164. // strutures. Free memory and invalidate the info.
  165. //
  166. return NO_ERROR;
  167. }
  168. DWORD
  169. WINAPI
  170. SrvrMonitor(
  171. IN LPCWSTR pwszMachine,
  172. IN OUT LPWSTR *ppwcArguments,
  173. IN DWORD dwArgCount,
  174. IN DWORD dwFlags,
  175. IN LPCVOID pvData,
  176. OUT LPWSTR pwcNewContext
  177. )
  178. {
  179. DWORD dwError = NO_ERROR;
  180. DWORD dwIndex, i, j, k;
  181. BOOL bFound = FALSE;
  182. PFN_HANDLE_CMD pfnHandler = NULL;
  183. PNS_CONTEXT_ENTRY_FN pfnHelperEntryPt;
  184. PNS_CONTEXT_DUMP_FN pfnHelperDumpPt;
  185. WCHAR wcSvrTemp[MAX_IP_STRING_LEN+1] = {L'\0'};
  186. BOOL fTemp = FALSE;
  187. DWORD dwNumMatched;
  188. DWORD dwCmdHelpToken = 0;
  189. if(dwArgCount is 1)
  190. {
  191. //Hmm! Hasn't passed any Server Info. Set the server to the value pwsRouter
  192. WCHAR ServerInfo[1000] = L"\\\\";
  193. if( pwszMachine and
  194. wcslen(pwszMachine) > 2 and
  195. wcsncmp(pwszMachine, L"\\\\", 2) and
  196. IsIpAddress(pwszMachine) is FALSE )
  197. {
  198. wcscpy(&ServerInfo[2], pwszMachine );
  199. pwszMachine = ServerInfo;
  200. }
  201. if( FALSE is SetServerInfo(pwszMachine) )
  202. {
  203. return GetLastError();
  204. }
  205. else
  206. {
  207. // wcscpy(pwcNewContext, L"wins server");
  208. g_fServer = TRUE;
  209. return ERROR_CONTEXT_SWITCH;
  210. }
  211. }
  212. dwIndex = 1;
  213. //If it is a help for server command
  214. if( IsHelpToken(ppwcArguments[dwIndex]) is TRUE and
  215. g_fServer is FALSE )
  216. {
  217. DisplayMessage(g_hModule, HLP_WINS_CONTEXT_SERVER_EX);
  218. dwError = NO_ERROR;
  219. goto CleanUp;
  220. }
  221. //Is it a servername or address?
  222. if( IsValidServer(ppwcArguments[dwIndex]) )
  223. {
  224. if( g_fServer is TRUE and
  225. dwArgCount > 2 )
  226. {
  227. wcscpy(wcSvrTemp, g_ServerIpAddressUnicodeString);
  228. fTemp = TRUE;
  229. }
  230. if( FALSE is SetServerInfo(ppwcArguments[dwIndex]) )
  231. {
  232. dwError = GetLastError();
  233. goto CleanUp;
  234. }
  235. pwcNewContext[wcslen(pwcNewContext)- wcslen(ppwcArguments[dwIndex]) -1 ] = L'\0';
  236. dwIndex++;
  237. g_fServer = TRUE;
  238. }
  239. else if( g_fServer is FALSE )
  240. {
  241. WCHAR ServerInfo[1000] = L"\\\\";
  242. if( pwszMachine and
  243. wcslen(pwszMachine) > 2 and
  244. wcsncmp(pwszMachine, L"\\\\", 2) and
  245. IsIpAddress(pwszMachine) is FALSE )
  246. {
  247. wcscpy(&ServerInfo[2], pwszMachine );
  248. pwszMachine = ServerInfo;
  249. }
  250. if( FALSE is SetServerInfo(pwszMachine) )
  251. {
  252. dwError = GetLastError();
  253. goto CleanUp;
  254. }
  255. g_fServer = TRUE;
  256. }
  257. if( dwIndex >= dwArgCount )
  258. {
  259. dwError = ERROR_CONTEXT_SWITCH;
  260. //wcscpy(pwcNewContext, L"wins server");
  261. goto CleanUp;
  262. }
  263. //Is it a top level(non Group command)?
  264. for(i=0; i<g_ulSrvrNumTopCmds; i++)
  265. {
  266. if(MatchToken(ppwcArguments[dwIndex],
  267. g_SrvrCmds[i].pwszCmdToken))
  268. {
  269. bFound = TRUE;
  270. pfnHandler = g_SrvrCmds[i].pfnCmdHandler;
  271. dwCmdHelpToken = g_SrvrCmds[i].dwCmdHlpToken;
  272. dwIndex++;
  273. break;
  274. }
  275. }
  276. if(bFound)
  277. {
  278. if(dwArgCount > 3 && IsHelpToken(ppwcArguments[dwIndex]))
  279. {
  280. DisplayMessage(g_hModule, dwCmdHelpToken);
  281. dwError = NO_ERROR;
  282. goto CleanUp;
  283. }
  284. dwIndex++;
  285. dwError = (*pfnHandler)(pwszMachine, ppwcArguments, dwIndex, dwArgCount,
  286. dwFlags, pvData, &bFound);
  287. goto CleanUp;
  288. }
  289. bFound = FALSE;
  290. //It is not a non Group Command. Then is it a config command for the manager?
  291. for(i = 0; (i < g_ulSrvrNumGroups) and !bFound; i++)
  292. {
  293. if(MatchToken(ppwcArguments[dwIndex],
  294. g_SrvrCmdGroups[i].pwszCmdGroupToken))
  295. {
  296. //
  297. // Command matched entry i, so look at the table of sub commands
  298. // for this command
  299. //
  300. if( dwArgCount > dwIndex+1 )
  301. {
  302. for (j = 0; j < g_SrvrCmdGroups[i].ulCmdGroupSize; j++)
  303. {
  304. if (MatchCmdLine(ppwcArguments+dwIndex,
  305. dwArgCount - 1,
  306. g_SrvrCmdGroups[i].pCmdGroup[j].pwszCmdToken,
  307. &dwNumMatched))
  308. {
  309. bFound = TRUE;
  310. pfnHandler = g_SrvrCmdGroups[i].pCmdGroup[j].pfnCmdHandler;
  311. dwCmdHelpToken = g_SrvrCmdGroups[i].pCmdGroup[j].dwCmdHlpToken;
  312. //
  313. // break out of the for(j) loop
  314. //
  315. dwIndex+=dwNumMatched;
  316. break;
  317. }
  318. }
  319. }
  320. if(!bFound)
  321. {
  322. //
  323. // We matched the command group token but none of the
  324. // sub commands
  325. //
  326. DisplayMessage(g_hModule,
  327. EMSG_WINS_INCOMPLETE_COMMAND);
  328. for (j = 0; j < g_SrvrCmdGroups[i].ulCmdGroupSize; j++)
  329. {
  330. DisplayMessage(g_hModule,
  331. g_SrvrCmdGroups[i].pCmdGroup[j].dwShortCmdHelpToken);
  332. DisplayMessage(g_hModule, WINS_FORMAT_LINE);
  333. }
  334. dwError = ERROR_INVALID_PARAMETER;
  335. goto CleanUp;
  336. }
  337. else
  338. {
  339. //
  340. // quit the for(i)
  341. //
  342. break;
  343. }
  344. }
  345. }
  346. if (!bFound)
  347. {
  348. //
  349. // Command not found.
  350. //
  351. if( _wcsicmp(ppwcArguments[dwIndex], L"..") is 0 )
  352. {
  353. if(g_ServerNameUnicode)
  354. {
  355. WinsFreeMemory(g_ServerNameUnicode);
  356. g_ServerNameUnicode = NULL;
  357. }
  358. memset(g_ServerIpAddressUnicodeString, 0x00, (MAX_IP_STRING_LEN+1)*sizeof(WCHAR));
  359. memset(g_ServerIpAddressAnsiString, 0x00, (MAX_IP_STRING_LEN+1)*sizeof(CHAR));
  360. g_fServer = FALSE;
  361. }
  362. dwError = ERROR_CMD_NOT_FOUND;
  363. goto CleanUp;
  364. }
  365. //
  366. // See if it is a request for help.
  367. //
  368. if (dwNumMatched < (dwArgCount - 1) and
  369. wcslen(ppwcArguments[dwNumMatched+1]) > 0 and
  370. IsHelpToken(ppwcArguments[dwNumMatched + 1]))
  371. {
  372. DisplayMessage(g_hModule, dwCmdHelpToken);
  373. dwError = NO_ERROR;
  374. goto CleanUp;
  375. }
  376. //
  377. // Call the parsing routine for the command
  378. //
  379. dwError = (*pfnHandler)(pwszMachine, ppwcArguments, dwIndex,
  380. dwArgCount, dwFlags, pvData, &bFound);
  381. if( dwError is ERROR_CONTEXT_SWITCH )
  382. {
  383. goto CleanUp;
  384. }
  385. CleanUp:
  386. if( fTemp )
  387. {
  388. fTemp = SetServerInfo(wcSvrTemp);
  389. }
  390. return dwError;
  391. }
  392. DWORD
  393. WINAPI
  394. SrvrUnInit(
  395. IN DWORD dwReserved
  396. )
  397. {
  398. return NO_ERROR;
  399. }
  400. BOOL
  401. SetServerInfo(
  402. IN LPCWSTR pwszServerInfo
  403. )
  404. {
  405. BOOL fReturn = TRUE;
  406. DWORD dwComputerNameLen = 0;
  407. LPSTR pszComputerName = NULL;
  408. LPWSTR pwszComputerName = NULL;
  409. struct hostent * lpHostEnt = NULL;
  410. BYTE pbAdd[4];
  411. char szAdd[4];
  412. DWORD dwHostName = 0;
  413. DWORD dwIpAddress = 0;
  414. DWORD i = 0, nLen = 0;
  415. DWORD Status = 0;
  416. DWORD Access = 0;
  417. CHAR *pTemp = NULL,
  418. *pTemp1 = NULL;
  419. DWORD dwTempLen = 0;
  420. CHAR cTempIp[MAX_IP_STRING_LEN+1] = {L'\0'};
  421. WCHAR wTempIp[MAX_IP_STRING_LEN+1] = {'\0'};
  422. LPWSTR pwszTempServer = NULL;
  423. LPSTR pszTempServer = NULL;
  424. WCHAR wTempNetBios[MAX_COMPUTER_NAME_LEN] = {L'\0'};
  425. handle_t hTempBind = NULL;
  426. WINSINTF_BIND_DATA_T TempBindData;
  427. LPWSTR pwsz = NULL;
  428. LPSTR psz = NULL;
  429. if( g_ServerNameUnicode isnot NULL and
  430. IsIpAddress(g_ServerIpAddressUnicodeString) is TRUE )
  431. {
  432. strcpy(cTempIp, g_ServerIpAddressAnsiString);
  433. wcscpy(wTempIp, g_ServerIpAddressUnicodeString);
  434. pwszTempServer = g_ServerNameUnicode;
  435. pszTempServer = g_ServerNameAnsi;
  436. wcscpy(wTempNetBios, g_ServerNetBiosName);
  437. }
  438. memset(g_ServerNetBiosName, 0x00, MAX_COMPUTER_NAME_LEN*sizeof(WCHAR));
  439. g_ServerNetBiosName[0] = L'\\';
  440. g_ServerNetBiosName[1] = L'\\';
  441. if( pwszServerInfo is NULL )
  442. {
  443. if( !GetComputerNameEx(ComputerNameDnsFullyQualified,
  444. NULL,
  445. &dwComputerNameLen) )
  446. {
  447. pwszComputerName = WinsAllocateMemory((dwComputerNameLen+1)*sizeof(WCHAR));
  448. if(pwszComputerName is NULL)
  449. {
  450. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  451. fReturn = FALSE;
  452. goto RETURN;
  453. }
  454. dwComputerNameLen++;
  455. if( !GetComputerNameEx(ComputerNameDnsFullyQualified,
  456. pwszComputerName,
  457. &dwComputerNameLen) )
  458. {
  459. fReturn = FALSE;
  460. goto RETURN;
  461. }
  462. }
  463. else
  464. {
  465. fReturn = FALSE;
  466. goto RETURN;
  467. }
  468. }
  469. else //might be a computer name or IP address. Determine what.
  470. {
  471. if( wcslen(pwszServerInfo) > 2 and
  472. _wcsnicmp(pwszServerInfo, L"\\\\", 2) is 0 )
  473. {
  474. pwszComputerName = WinsAllocateMemory((wcslen(pwszServerInfo) - 1)*sizeof(WCHAR));
  475. if( pwszComputerName is NULL )
  476. {
  477. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  478. fReturn = FALSE;
  479. goto RETURN;
  480. }
  481. memcpy(pwszComputerName,
  482. (pwszServerInfo+2),
  483. (wcslen(pwszServerInfo) - 1)*sizeof(WCHAR));
  484. }
  485. else if( IsIpAddress(pwszServerInfo) is TRUE ) //it is an IP Address
  486. {
  487. memset(g_ServerIpAddressUnicodeString, 0x00,
  488. (MAX_IP_STRING_LEN+1)*sizeof(WCHAR));
  489. wcscpy(g_ServerIpAddressUnicodeString,
  490. pwszServerInfo);
  491. memset(g_ServerIpAddressAnsiString, 0x00,
  492. (MAX_IP_STRING_LEN+1)*sizeof(CHAR));
  493. psz = WinsUnicodeToAnsi(g_ServerIpAddressUnicodeString, NULL);
  494. if( psz )
  495. {
  496. strcpy(g_ServerIpAddressAnsiString, psz);
  497. WinsFreeMemory(psz);
  498. psz = NULL;
  499. }
  500. dwIpAddress = inet_addr(g_ServerIpAddressAnsiString);
  501. lpHostEnt = gethostbyaddr((char *)&dwIpAddress, 4, AF_INET);
  502. if( lpHostEnt is NULL )
  503. {
  504. DisplayMessage(g_hModule, EMSG_WINS_UNKNOWN_SERVER);
  505. fReturn = FALSE;
  506. goto RETURN;
  507. }
  508. dwHostName = strlen(lpHostEnt->h_name);
  509. g_ServerNameAnsi = WinsAllocateMemory(dwHostName+1);
  510. if (g_ServerNameAnsi == NULL)
  511. goto RETURN;
  512. strcpy(g_ServerNameAnsi, lpHostEnt->h_name);
  513. g_ServerNameUnicode = WinsAllocateMemory((dwHostName+1)*sizeof(WCHAR));
  514. wcscpy(g_ServerNameUnicode, WinsAnsiToUnicode(lpHostEnt->h_name, NULL));
  515. //Bind the server
  516. if( hTempBind )
  517. {
  518. //Unbind any previous server
  519. WinsUnbind(&TempBindData, hTempBind);
  520. hTempBind = NULL;
  521. }
  522. TempBindData.fTcpIp = TRUE;
  523. TempBindData.pServerAdd = (LPBYTE)g_ServerIpAddressUnicodeString;
  524. TempBindData.pPipeName = (LPBYTE)g_ServerNameUnicode;
  525. hTempBind = WinsBind(&TempBindData);
  526. if (hTempBind == NULL)
  527. {
  528. DisplayMessage(g_hModule,
  529. EMSG_WINS_BIND_FAILED,
  530. g_ServerNameUnicode);
  531. fReturn = FALSE;
  532. goto RETURN;
  533. }
  534. //Make sure this is the correct address for the WINS
  535. {
  536. WINSINTF_ADD_T WinsAdd = {0};
  537. UCHAR pName[256] = {'\0'};
  538. Status = WinsGetNameAndAdd(hTempBind,
  539. &WinsAdd,
  540. pName);
  541. if( Status is NO_ERROR )
  542. {
  543. LPWSTR pAdd = IpAddressToString(WinsAdd.IPAdd);
  544. if( pAdd is NULL )
  545. {
  546. fReturn = FALSE;
  547. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  548. goto RETURN;
  549. }
  550. wcscpy(g_ServerIpAddressUnicodeString,
  551. pAdd);
  552. psz = WinsUnicodeToAnsi(g_ServerIpAddressUnicodeString, NULL);
  553. if( psz )
  554. {
  555. strcpy(g_ServerIpAddressAnsiString, psz );
  556. WinsFreeMemory(psz);
  557. psz = NULL;
  558. }
  559. pwsz = WinsAnsiToUnicode(pName, NULL);
  560. if( pwsz )
  561. {
  562. wcscpy( g_ServerNetBiosName+2, g_ServerIpAddressUnicodeString);
  563. WinsFreeMemory(pwsz);
  564. pwsz = NULL;
  565. }
  566. else
  567. {
  568. DisplayMessage(g_hModule,
  569. EMSG_WINS_OUT_OF_MEMORY);
  570. fReturn = FALSE;
  571. WinsFreeMemory(pAdd);
  572. pAdd = NULL;
  573. goto RETURN;
  574. }
  575. WinsFreeMemory(pAdd);
  576. pAdd = NULL;
  577. }
  578. else
  579. {
  580. DisplayErrorMessage(EMSG_WINS_VERIFY_ADDRESS,
  581. Status);
  582. }
  583. }
  584. //find out what type of access do we have
  585. Access = WINS_NO_ACCESS;
  586. Status = WinsCheckAccess(hTempBind, &Access);
  587. if( Status is RPC_S_PROCNUM_OUT_OF_RANGE )
  588. {
  589. DisplayMessage(g_hModule,
  590. EMSG_ACCESS_NOT_DETERMINED,
  591. g_ServerNameUnicode);
  592. if( g_hBind )
  593. {
  594. WinsUnbind(&g_BindData, g_hBind);
  595. }
  596. g_hBind = hTempBind;
  597. g_BindData = TempBindData;
  598. fReturn = TRUE;
  599. goto RETURN;
  600. }
  601. else if (WINSINTF_SUCCESS == Status)
  602. {
  603. if (Access != WINS_NO_ACCESS)
  604. {
  605. DisplayMessage(g_hModule,
  606. Access == WINS_CONTROL_ACCESS ? MSG_WINS_RWACCESS : MSG_WINS_ROACCESS,
  607. g_ServerNameUnicode);
  608. }
  609. if( Access is WINS_NO_ACCESS )
  610. {
  611. DisplayMessage(g_hModule,
  612. MSG_WINS_NOACCESS,
  613. g_ServerNameUnicode);
  614. fReturn = FALSE;
  615. goto RETURN;
  616. }
  617. if( g_hBind )
  618. {
  619. WinsUnbind(&g_BindData, g_hBind);
  620. }
  621. g_hBind = hTempBind;
  622. g_BindData = TempBindData;
  623. fReturn = TRUE;
  624. goto RETURN;
  625. }
  626. else
  627. {
  628. DisplayErrorMessage(EMSG_WINS_GETSTATUS_FAILED,
  629. Status);
  630. {
  631. fReturn = FALSE;
  632. goto RETURN;
  633. }
  634. }
  635. fReturn = TRUE;
  636. goto RETURN;
  637. }
  638. }
  639. //Now process the Computer name and convert it to ANSI because
  640. //gethostbyname requires ANSI character string.
  641. {
  642. int dw = 0;
  643. dw = WideCharToMultiByte(CP_ACP,
  644. 0,
  645. pwszComputerName,
  646. wcslen(pwszComputerName),
  647. pszComputerName,
  648. 0,
  649. NULL,
  650. NULL);
  651. if( dw is 0 )
  652. {
  653. fReturn = FALSE;
  654. return FALSE;
  655. }
  656. pszComputerName = WinsAllocateMemory((dw+1)*sizeof(CHAR));
  657. if( pszComputerName is NULL )
  658. {
  659. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  660. fReturn = FALSE;
  661. goto RETURN;
  662. }
  663. dw = WideCharToMultiByte(CP_ACP,
  664. 0,
  665. pwszComputerName,
  666. wcslen(pwszComputerName),
  667. pszComputerName,
  668. dw+1,
  669. NULL,
  670. NULL);
  671. if( dw is 0 )
  672. {
  673. fReturn = FALSE;
  674. goto RETURN;
  675. }
  676. }
  677. //Now get the server IP Address
  678. lpHostEnt = gethostbyname(pszComputerName);
  679. //Not a valid server name
  680. if( lpHostEnt is NULL )
  681. {
  682. DisplayMessage(g_hModule, EMSG_WINS_INVALID_COMPUTERNAME);
  683. if( pszComputerName )
  684. {
  685. WinsFreeMemory(pszComputerName);
  686. pszComputerName = NULL;
  687. }
  688. fReturn = FALSE;
  689. goto RETURN;
  690. }
  691. //Get the IP Address from the returned struct...
  692. memcpy(pbAdd, lpHostEnt->h_addr_list[0], 4);
  693. nLen = 0;
  694. for( i=0; i<4; i++)
  695. {
  696. _itoa((int)pbAdd[i], szAdd, 10);
  697. memcpy(g_ServerIpAddressAnsiString+nLen, szAdd, strlen(szAdd));
  698. nLen += strlen(szAdd);
  699. *(g_ServerIpAddressAnsiString+nLen) = '.';
  700. nLen++;
  701. }
  702. *(g_ServerIpAddressAnsiString+nLen-1) = '\0';
  703. dwIpAddress = WinsDottedStringToIpAddress(g_ServerIpAddressAnsiString);
  704. dwHostName = strlen(g_ServerIpAddressAnsiString);
  705. //Convert IP Address to a Unicode string and store it to the global variable.
  706. memset(g_ServerIpAddressUnicodeString, 0x00, (MAX_IP_STRING_LEN+1)*sizeof(WCHAR));
  707. pwsz = WinsAnsiToUnicode(g_ServerIpAddressAnsiString, NULL);
  708. if( pwsz )
  709. {
  710. wcscpy(g_ServerIpAddressUnicodeString, pwsz);
  711. WinsFreeMemory(pwsz);
  712. pwsz = NULL;
  713. }
  714. else
  715. {
  716. DisplayMessage(g_hModule,
  717. EMSG_WINS_OUT_OF_MEMORY);
  718. fReturn = FALSE;
  719. goto RETURN;
  720. }
  721. g_ServerNameAnsi = WinsAllocateMemory(strlen(lpHostEnt->h_name)+1);
  722. g_ServerNameUnicode = WinsAllocateMemory((strlen(lpHostEnt->h_name) + 1)*sizeof(WCHAR));
  723. if( ( g_ServerNameUnicode is NULL ) or
  724. ( g_ServerNameAnsi is NULL ) )
  725. {
  726. DisplayMessage(g_hModule, EMSG_WINS_NOT_ENOUGH_MEMORY);
  727. if( pszComputerName )
  728. {
  729. WinsFreeMemory(pszComputerName);
  730. pszComputerName = NULL;
  731. }
  732. if( pwszComputerName )
  733. {
  734. WinsFreeMemory(pwszComputerName);
  735. pwszComputerName = NULL;
  736. }
  737. fReturn = FALSE;
  738. goto RETURN;
  739. }
  740. strcpy(g_ServerNameAnsi, lpHostEnt->h_name);
  741. {
  742. int dw = 0;
  743. dw = MultiByteToWideChar(CP_ACP,
  744. 0,
  745. lpHostEnt->h_name,
  746. strlen(lpHostEnt->h_name),
  747. g_ServerNameUnicode,
  748. strlen(lpHostEnt->h_name)+1);
  749. if ( dw == 0 )
  750. {
  751. fReturn = FALSE;
  752. goto RETURN;
  753. }
  754. }
  755. //Bind the server
  756. TempBindData.fTcpIp = TRUE;
  757. TempBindData.pServerAdd = (LPBYTE)g_ServerIpAddressUnicodeString;
  758. TempBindData.pPipeName = (LPBYTE)g_ServerNameUnicode;
  759. hTempBind = WinsBind(&TempBindData);
  760. if (hTempBind == NULL)
  761. {
  762. DisplayMessage(g_hModule,
  763. EMSG_WINS_BIND_FAILED,
  764. g_ServerIpAddressUnicodeString);
  765. SetLastError(ERROR_INVALID_PARAMETER);
  766. fReturn = FALSE;
  767. goto RETURN;
  768. }
  769. //Make sure this is the correct address for the WINS
  770. {
  771. WINSINTF_ADD_T WinsAdd = {0};
  772. UCHAR pName[256] = {'\0'};
  773. Status = WinsGetNameAndAdd(hTempBind,
  774. &WinsAdd,
  775. pName);
  776. if( Status is NO_ERROR )
  777. {
  778. LPWSTR pAdd = IpAddressToString(WinsAdd.IPAdd);
  779. if( pAdd is NULL )
  780. {
  781. fReturn = FALSE;
  782. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  783. goto RETURN;
  784. }
  785. wcscpy(g_ServerIpAddressUnicodeString,
  786. pAdd);
  787. psz = WinsUnicodeToOem(pAdd, NULL);
  788. if( psz )
  789. {
  790. strcpy(g_ServerIpAddressAnsiString, psz);
  791. WinsFreeMemory(psz);
  792. psz = NULL;
  793. }
  794. pwsz = WinsOemToUnicode(pName, NULL);
  795. if( pwsz )
  796. {
  797. wcscpy( g_ServerNetBiosName+2, g_ServerIpAddressUnicodeString);
  798. WinsFreeMemory(pwsz);
  799. pwsz = NULL;
  800. }
  801. else
  802. {
  803. DisplayMessage(g_hModule,
  804. EMSG_WINS_OUT_OF_MEMORY);
  805. fReturn = FALSE;
  806. goto RETURN;
  807. }
  808. WinsFreeMemory(pAdd);
  809. pAdd = NULL;
  810. }
  811. else
  812. {
  813. DisplayErrorMessage(EMSG_WINS_VERIFY_ADDRESS,
  814. Status);
  815. }
  816. }
  817. //find out what type of access do we have
  818. Access = WINS_NO_ACCESS;
  819. Status = WinsCheckAccess(hTempBind, &Access);
  820. if( Status is RPC_S_PROCNUM_OUT_OF_RANGE )
  821. {
  822. DisplayMessage(g_hModule,
  823. EMSG_ACCESS_NOT_DETERMINED,
  824. g_ServerNameUnicode);
  825. if( g_hBind )
  826. {
  827. WinsUnbind(&g_BindData, g_hBind);
  828. }
  829. g_hBind = hTempBind;
  830. g_BindData = TempBindData;
  831. fReturn = TRUE;
  832. goto RETURN;
  833. }
  834. else if (WINSINTF_SUCCESS == Status)
  835. {
  836. if (Access != WINS_NO_ACCESS)
  837. {
  838. DisplayMessage(g_hModule,
  839. Access == WINS_CONTROL_ACCESS ? MSG_WINS_RWACCESS : MSG_WINS_ROACCESS,
  840. g_ServerNameUnicode);
  841. }
  842. if( Access is WINS_NO_ACCESS )
  843. {
  844. DisplayMessage(g_hModule,
  845. MSG_WINS_NOACCESS,
  846. g_ServerNameUnicode);
  847. fReturn = FALSE;
  848. goto RETURN;
  849. }
  850. if( g_hBind )
  851. {
  852. WinsUnbind(&g_BindData, g_hBind);
  853. }
  854. g_hBind = hTempBind;
  855. g_BindData = TempBindData;
  856. fReturn = TRUE;
  857. goto RETURN;
  858. }
  859. else
  860. {
  861. DisplayErrorMessage(EMSG_WINS_GETSTATUS_FAILED,
  862. Status);
  863. fReturn = FALSE;
  864. goto RETURN;
  865. }
  866. RETURN:
  867. if( pszComputerName)
  868. {
  869. WinsFreeMemory(pszComputerName);
  870. pszComputerName = NULL;
  871. }
  872. if( pwszComputerName )
  873. {
  874. WinsFreeMemory(pwszComputerName);
  875. pwszComputerName = NULL;
  876. }
  877. if( pTemp1 )
  878. {
  879. WinsFreeMemory(pTemp1);
  880. pTemp1 = NULL;
  881. }
  882. if( fReturn is FALSE )
  883. {
  884. wcscpy(g_ServerIpAddressUnicodeString, wTempIp);
  885. strcpy(g_ServerIpAddressAnsiString, cTempIp);
  886. g_ServerNameUnicode = pwszTempServer;
  887. g_ServerNameAnsi = pszTempServer;
  888. wcscpy(g_ServerNetBiosName, wTempNetBios);
  889. }
  890. return fReturn;
  891. }