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.

385 lines
10 KiB

  1. #include <windows.h>
  2. #include <stdlib.h>
  3. //#include <assert.h>
  4. #include <stdio.h>
  5. #include <netsh.h>
  6. #include <Iphlpapi.h>
  7. #include <Winsock2.h>
  8. #include <selbinding.hxx>
  9. #include <skeleton.h>
  10. #include <handlers.hxx>
  11. extern HANDLE g_hModule;
  12. #define MIN(x, y) ( ((x) >= (y)) ? y:x )
  13. #define HandleErrorGeneric(_dwStatus) \
  14. {\
  15. switch (_dwStatus)\
  16. {\
  17. case ERROR_SUCCESS:\
  18. break;\
  19. case ERROR_ACCESS_DENIED:\
  20. PrintError(g_hModule, ERRORMSG_ACCESSDENIED);\
  21. break;\
  22. case ERROR_OUTOFMEMORY:\
  23. PrintError(g_hModule, ERRORMSG_ACCESSDENIED);\
  24. break;\
  25. case ERROR_INVALID_DATA:\
  26. PrintError(g_hModule, ERRORMSG_INVALIDDATA);\
  27. default:\
  28. PrintError(g_hModule, ERRORMSG_UNKNOWN);\
  29. }\
  30. }
  31. BOOL WINAPI CheckServerOrGreater(
  32. IN UINT CIMOSType,
  33. IN UINT CIMOSProductSuite,
  34. IN LPCWSTR CIMOSVersion,
  35. IN LPCWSTR CIMOSBuildNumber,
  36. IN LPCWSTR CIMServicePackMajorVersion,
  37. IN LPCWSTR CIMServicePackMinorVersion,
  38. IN UINT CIMProcessorArchitecture,
  39. IN DWORD dwReserved
  40. )
  41. {
  42. if (_wtoi(CIMOSBuildNumber) > 3000)
  43. {
  44. return TRUE;
  45. }
  46. return FALSE;
  47. }
  48. DWORD
  49. HandleShowSettings()
  50. {
  51. DWORD dwStatus = ERROR_SUCCESS;
  52. SB_VER sbVer = SB_VER_UNKNOWN;
  53. LPVOID lpSettings = NULL;
  54. DWORD dwSize = 0;
  55. VER_SUBNETS_SETTINGS *pSubnetSettings = NULL;
  56. dwStatus = GetSelectiveBindingSettings(&sbVer, &dwSize, &lpSettings);
  57. if (dwStatus != ERROR_SUCCESS)
  58. {
  59. HandleErrorGeneric(dwStatus);
  60. return dwStatus;
  61. }
  62. if (sbVer == SB_VER_DEFAULT)
  63. {
  64. PrintMessage(L"Default\n");
  65. return ERROR_SUCCESS;
  66. }
  67. switch (sbVer)
  68. {
  69. case SB_VER_SUBNETS:
  70. pSubnetSettings = (VER_SUBNETS_SETTINGS *) lpSettings;
  71. if (pSubnetSettings->bAdmit)
  72. {
  73. PrintMessage(L"Add List\n");
  74. }
  75. else
  76. {
  77. PrintMessage(L"Delete List\n");
  78. }
  79. for (DWORD idx = 0; idx < pSubnetSettings->dwCount; idx++)
  80. {
  81. PrintMessage(L"%1!S!\n",inet_ntoa(*((struct in_addr*)&(pSubnetSettings->dwSubnets[idx]))));
  82. }
  83. break;
  84. case SB_VER_INDICES:
  85. case SB_VER_UNKNOWN:
  86. PrintMessage(L"Unknown selective binding format\n");
  87. break;
  88. default:
  89. //assert(0);
  90. PrintMessage(L"Unknown selective binding format\n");
  91. }
  92. delete [] lpSettings;
  93. return ERROR_SUCCESS;
  94. }
  95. DWORD
  96. HandleShowInterfaces()
  97. {
  98. DWORD dwStatus = ERROR_SUCCESS;
  99. SB_VER sbVer = SB_VER_UNKNOWN;
  100. LPVOID lpSettings = NULL;
  101. DWORD dwDummy = 0;
  102. VER_SUBNETS_SETTINGS *pSubnetSettings = NULL;
  103. PMIB_IPADDRTABLE pIpAddrTable = NULL;
  104. DWORD dwSize = 0;
  105. dwStatus = GetSelectiveBindingSettings(&sbVer, &dwDummy, &lpSettings);
  106. if (dwStatus != ERROR_SUCCESS)
  107. {
  108. HandleErrorGeneric(dwStatus);
  109. return dwStatus;
  110. }
  111. if ((sbVer != SB_VER_SUBNETS)&&(lpSettings != NULL))
  112. {
  113. PrintMessage(L"Unknown selective binding format\n");
  114. return -1;
  115. }
  116. pSubnetSettings = (VER_SUBNETS_SETTINGS *) lpSettings;
  117. // Query for size
  118. dwStatus = GetIpAddrTable(NULL,
  119. &dwSize,
  120. TRUE);
  121. if (dwStatus != ERROR_INSUFFICIENT_BUFFER)
  122. {
  123. //assert(0);
  124. HandleErrorGeneric(dwStatus);
  125. return dwStatus;
  126. }
  127. pIpAddrTable = (PMIB_IPADDRTABLE) new char [dwSize];
  128. if (pIpAddrTable == NULL)
  129. {
  130. PrintError(g_hModule, ERRORMSG_OOM);
  131. return -1;
  132. }
  133. // Get the interfaces for the machine
  134. dwStatus = GetIpAddrTable(pIpAddrTable,
  135. &dwSize,
  136. TRUE);
  137. if (dwStatus != ERROR_SUCCESS)
  138. {
  139. HandleErrorGeneric(dwStatus);
  140. delete [] pIpAddrTable;
  141. return -1;
  142. }
  143. // Print out the table header
  144. PrintMessage(L"\nSubnet Interface Status Description\n\n");
  145. // Iterate over the interfaces on the system
  146. for (DWORD idx = 0; idx < pIpAddrTable->dwNumEntries; idx++)
  147. {
  148. PMIB_IPADDRROW pRow = &pIpAddrTable->table[idx];
  149. struct in_addr addr;
  150. addr.S_un.S_addr = pRow->dwAddr & pRow->dwMask;
  151. PrintMessage(L"%1!-16S!",inet_ntoa(addr));
  152. addr.S_un.S_addr = pRow->dwAddr;
  153. PrintMessage(L"%1!-16S!",inet_ntoa(addr));
  154. // Check if its enabled or disabled, if we have default settings, we know its enabled
  155. BOOL bEnabled;
  156. if (pSubnetSettings == NULL)
  157. {
  158. bEnabled = TRUE;
  159. }
  160. else
  161. {
  162. bEnabled = !pSubnetSettings->bAdmit;
  163. for (DWORD idx2 = 0; idx2 < pSubnetSettings->dwCount; idx2++)
  164. {
  165. DWORD dwSubnet = pRow->dwAddr & pRow->dwMask;
  166. if (dwSubnet == pSubnetSettings->dwSubnets[idx2])
  167. {
  168. bEnabled = !bEnabled;
  169. break;
  170. }
  171. }
  172. }
  173. if (bEnabled)
  174. {
  175. PrintMessage(L"%1!-9s!",L"Enabled");
  176. }
  177. else
  178. {
  179. PrintMessage(L"%1!-9s!",L"Disabled");
  180. }
  181. // Print the description
  182. MIB_IFROW IfRow;
  183. memset(&IfRow,0,sizeof(MIB_IFROW));
  184. IfRow.dwIndex = pRow->dwIndex;
  185. GetIfEntry(&IfRow);
  186. CHAR szBuff[40];
  187. memset(szBuff,0,40);
  188. DWORD dwDescIdx = 0;
  189. DWORD dwLenToCopy = 0;
  190. BOOL bFirst = TRUE;
  191. while(dwDescIdx < IfRow.dwDescrLen)
  192. {
  193. dwLenToCopy = MIN(38,IfRow.dwDescrLen);
  194. memcpy(szBuff, &(IfRow.bDescr[dwDescIdx]), dwLenToCopy);
  195. dwDescIdx += dwLenToCopy;
  196. if (bFirst)
  197. {
  198. bFirst = FALSE;
  199. PrintMessage(L" %1!S!\n",szBuff);
  200. }
  201. else
  202. {
  203. PrintMessage(L" %1!S!\n",szBuff);
  204. }
  205. }
  206. PrintMessage(L"\n");
  207. }
  208. return ERROR_SUCCESS;
  209. }
  210. DWORD WINAPI
  211. HandleAddOrDelete(
  212. IN LPWSTR *ppwcArguments,
  213. IN DWORD dwCurrentIndex,
  214. IN DWORD dwArgCount,
  215. IN BOOL bAdd
  216. )
  217. {
  218. // Check the validity of the arguments passed:
  219. // 1 Must set at least one subnet
  220. // 2 Each subnet must be a valid dotted decimal string
  221. DWORD idx;
  222. DWORD dwStatus = ERROR_SUCCESS;
  223. LPDWORD lpAddr = NULL;
  224. DWORD dwAddrCount = dwArgCount - dwCurrentIndex;
  225. if (dwAddrCount == 0)
  226. {
  227. PrintError(g_hModule, ERRORMSG_ADD_1);
  228. return -1;
  229. }
  230. // Allocate an array to put all this addresses in, just go
  231. // straight for the heap, don't bother trying to fit it in a
  232. // stack based array first
  233. lpAddr = new DWORD[dwAddrCount];
  234. if (!lpAddr)
  235. {
  236. PrintError(g_hModule, ERRORMSG_OOM);
  237. return -1;
  238. }
  239. for (idx = 0; idx < dwAddrCount; idx++)
  240. {
  241. // create a single byte char string from this wc string (for inet_addr).
  242. DWORD dwLen = wcslen(ppwcArguments[idx+dwCurrentIndex]);
  243. CHAR *pTmp = new char[dwLen+1];
  244. if (pTmp == NULL)
  245. {
  246. PrintError(g_hModule, ERRORMSG_OOM);
  247. delete [] lpAddr;
  248. return -1;
  249. }
  250. _snprintf(pTmp, dwLen+1, "%S", ppwcArguments[idx+dwCurrentIndex]);
  251. lpAddr[idx] = inet_addr(pTmp);
  252. if (lpAddr[idx] == INADDR_NONE)
  253. {
  254. PrintError(g_hModule, ERRORMSG_ADD_2, pTmp);
  255. delete [] lpAddr;
  256. delete [] pTmp;
  257. return -1;
  258. }
  259. delete [] pTmp;
  260. }
  261. // Write this selective binding setting to the registry. Note: the
  262. // user can specify any subnets they want, we don't verify that they exist by design
  263. dwStatus = SetSelectiveBindingSubnets(dwAddrCount, lpAddr, bAdd);
  264. HandleErrorGeneric(dwStatus);
  265. delete [] lpAddr;
  266. return dwStatus;
  267. }
  268. DWORD WINAPI
  269. HandleAdd(
  270. IN LPCWSTR pwszMachine,
  271. IN LPWSTR *ppwcArguments,
  272. IN DWORD dwCurrentIndex,
  273. IN DWORD dwArgCount,
  274. IN DWORD dwFlags,
  275. IN LPCVOID pvData,
  276. OUT BOOL *pbDone)
  277. {
  278. return HandleAddOrDelete(ppwcArguments, dwCurrentIndex, dwArgCount, TRUE);
  279. }
  280. DWORD WINAPI
  281. HandleDelete(
  282. IN LPCWSTR pwszMachine,
  283. IN OUT LPWSTR *ppwcArguments,
  284. IN DWORD dwCurrentIndex,
  285. IN DWORD dwArgCount,
  286. IN DWORD dwFlags,
  287. IN LPCVOID pvData,
  288. OUT BOOL *pbDone)
  289. {
  290. return HandleAddOrDelete(ppwcArguments, dwCurrentIndex, dwArgCount, FALSE);
  291. }
  292. DWORD WINAPI
  293. HandleReset(
  294. IN LPCWSTR pwszMachine,
  295. IN OUT LPWSTR *ppwcArguments,
  296. IN DWORD dwCurrentIndex,
  297. IN DWORD dwArgCount,
  298. IN DWORD dwFlags,
  299. IN LPCVOID pvData,
  300. OUT BOOL *pbDone)
  301. {
  302. // All we do is delete the selective binding key to reset to default settings
  303. DWORD dwStatus;
  304. dwStatus = DeleteSelectiveBinding();
  305. HandleErrorGeneric(dwStatus);
  306. return dwStatus;
  307. }
  308. DWORD WINAPI
  309. HandleShow(
  310. IN LPCWSTR pwszMachine,
  311. IN LPWSTR *ppwcArguments,
  312. IN DWORD dwCurrentIndex,
  313. IN DWORD dwArgCount,
  314. IN DWORD dwFlags,
  315. IN LPCVOID pvData,
  316. OUT BOOL *pbDone)
  317. {
  318. if ((dwArgCount - dwCurrentIndex) != 1)
  319. {
  320. PrintError(g_hModule, HLP_SHOW);
  321. return -1;
  322. }
  323. if (MatchToken(ppwcArguments[dwCurrentIndex], L"settings"))
  324. {
  325. return HandleShowSettings();
  326. }
  327. else if (MatchToken(ppwcArguments[dwCurrentIndex], L"interfaces"))
  328. {
  329. return HandleShowInterfaces();
  330. }
  331. PrintError(g_hModule, HLP_SHOW);
  332. return -1;
  333. }