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.

373 lines
8.0 KiB

  1. /*++
  2. Copyright(c) 1995 Microsoft Corporation
  3. MODULE NAME
  4. misc.c
  5. ABSTRACT
  6. Miscellaneous routines for the automatic connection service.
  7. AUTHOR
  8. Anthony Discolo (adiscolo) 20-Mar-1995
  9. REVISION HISTORY
  10. Original version from Gurdeep
  11. --*/
  12. #define UNICODE
  13. #define _UNICODE
  14. #include <nt.h>
  15. #include <ntrtl.h>
  16. #include <nturtl.h>
  17. #include <stdlib.h>
  18. #include <windows.h>
  19. #include <stdio.h>
  20. #include <npapi.h>
  21. #include <winsock.h>
  22. #include <acd.h>
  23. #include <debug.h>
  24. #include <rasman.h>
  25. #include "access.h"
  26. #include "reg.h"
  27. #include "misc.h"
  28. #include "process.h"
  29. #include "rtutils.h"
  30. LPTSTR
  31. AddressToUnicodeString(
  32. IN PACD_ADDR pAddr
  33. )
  34. {
  35. PCHAR pch;
  36. struct in_addr in;
  37. // CHAR szBuf[64];
  38. CHAR *pszBuf = NULL;
  39. LPTSTR pszAddrOrig = NULL, pszAddr = NULL;
  40. INT i;
  41. INT cch;
  42. pszBuf = LocalAlloc(LPTR, 1024);
  43. if(NULL == pszBuf)
  44. {
  45. return NULL;
  46. }
  47. switch (pAddr->fType) {
  48. case ACD_ADDR_IP:
  49. in.s_addr = pAddr->ulIpaddr;
  50. pch = inet_ntoa(in);
  51. pszAddrOrig = AnsiStringToUnicodeString(pch, NULL, 0);
  52. break;
  53. case ACD_ADDR_IPX:
  54. NodeNumberToString(pAddr->cNode, pszBuf);
  55. pszAddrOrig = AnsiStringToUnicodeString(pszBuf, NULL, 0);
  56. break;
  57. case ACD_ADDR_NB:
  58. // RtlZeroMemory(&szBuf, sizeof (szBuf));
  59. pch = pszBuf;
  60. for (i = 0; i < 1024; i++) {
  61. if (pAddr->cNetbios[i] == ' ' || pAddr->cNetbios[i] == '\0')
  62. break;
  63. *pch++ = pAddr->cNetbios[i];
  64. }
  65. //
  66. // Make sure this is a string - there are penetration attack
  67. // tests in stress which pass bogus buffers.
  68. //
  69. pszBuf[1023] = '\0';
  70. pszAddrOrig = AnsiStringToUnicodeString(pszBuf, NULL, 0);
  71. break;
  72. case ACD_ADDR_INET:
  73. //
  74. // Make sure that the address is a string
  75. //
  76. pAddr->szInet[1023] = '\0';
  77. pszAddrOrig = AnsiStringToUnicodeString(pAddr->szInet, NULL, 0);
  78. break;
  79. default:
  80. RASAUTO_TRACE1("AddressToUnicodeString: unknown address type (%d)", pAddr->fType);
  81. break;
  82. }
  83. if (pszAddrOrig != NULL) {
  84. pszAddr = CanonicalizeAddress(pszAddrOrig);
  85. LocalFree(pszAddrOrig);
  86. }
  87. if(NULL != pszBuf)
  88. {
  89. LocalFree(pszBuf);
  90. }
  91. return pszAddr;
  92. } // AddressToUnicodeString
  93. LPTSTR
  94. CompareConnectionLists(
  95. IN LPTSTR *lpPreList,
  96. IN DWORD dwPreSize,
  97. IN LPTSTR *lpPostList,
  98. IN DWORD dwPostSize
  99. )
  100. {
  101. DWORD i, j;
  102. DWORD iMax, jMax;
  103. LPTSTR *piList, *pjList;
  104. BOOLEAN fFound;
  105. if (dwPostSize > dwPreSize) {
  106. iMax = dwPostSize;
  107. piList = lpPostList;
  108. jMax = dwPreSize;
  109. pjList = lpPreList;
  110. }
  111. else {
  112. iMax = dwPreSize;
  113. piList = lpPreList;
  114. jMax = dwPostSize;
  115. pjList = lpPostList;
  116. }
  117. //
  118. // If one list is empty, then return
  119. // the first entry of the other list.
  120. //
  121. if (iMax > 0 && jMax == 0)
  122. return piList[0];
  123. for (i = 0; i < iMax; i++) {
  124. fFound = FALSE;
  125. for (j = 0; j < jMax; j++) {
  126. if (!wcscmp(piList[i], pjList[j])) {
  127. fFound = TRUE;
  128. break;
  129. }
  130. }
  131. if (!fFound)
  132. return piList[i];
  133. }
  134. //
  135. // Didn't find any differences.
  136. //
  137. return NULL;
  138. } // CompareConnectionLists
  139. LPTSTR
  140. CopyString(
  141. IN LPTSTR pszString
  142. )
  143. {
  144. LPTSTR pszNewString;
  145. pszNewString = LocalAlloc(
  146. LPTR,
  147. (wcslen(pszString) + 1) * sizeof (TCHAR));
  148. if (pszNewString == NULL) {
  149. RASAUTO_TRACE("CopyString: LocalAlloc failed");
  150. return NULL;
  151. }
  152. wcscpy(pszNewString, pszString);
  153. return pszNewString;
  154. } // CopyString
  155. PCHAR
  156. UnicodeStringToAnsiString(
  157. IN PWCHAR pszUnicode,
  158. OUT PCHAR pszAnsi,
  159. IN USHORT cbAnsi
  160. )
  161. {
  162. NTSTATUS status;
  163. BOOLEAN fAllocate = (pszAnsi == NULL);
  164. UNICODE_STRING unicodeString;
  165. ANSI_STRING ansiString;
  166. RtlInitUnicodeString(&unicodeString, pszUnicode);
  167. if (pszAnsi != NULL) {
  168. ansiString.Length = 0;
  169. ansiString.MaximumLength = cbAnsi;
  170. ansiString.Buffer = pszAnsi;
  171. }
  172. status = RtlUnicodeStringToAnsiString(
  173. &ansiString,
  174. &unicodeString,
  175. fAllocate);
  176. return (status == STATUS_SUCCESS ? ansiString.Buffer : NULL);
  177. } // UnicodeStringToAnsiString
  178. PWCHAR
  179. AnsiStringToUnicodeString(
  180. IN PCHAR pszAnsi,
  181. OUT PWCHAR pszUnicode,
  182. IN USHORT cbUnicode
  183. )
  184. {
  185. NTSTATUS status;
  186. BOOLEAN fAllocate = (pszUnicode == NULL);
  187. UNICODE_STRING unicodeString;
  188. ANSI_STRING ansiString;
  189. RtlInitAnsiString(&ansiString, pszAnsi);
  190. if (pszUnicode != NULL) {
  191. unicodeString.Length = 0;
  192. unicodeString.MaximumLength = cbUnicode;
  193. unicodeString.Buffer = pszUnicode;
  194. }
  195. status = RtlAnsiStringToUnicodeString(
  196. &unicodeString,
  197. &ansiString,
  198. fAllocate);
  199. return (status == STATUS_SUCCESS ? unicodeString.Buffer : NULL);
  200. } // AnsiStringToUnicodeString
  201. VOID
  202. FreeStringArray(
  203. IN LPTSTR *lpEntries,
  204. IN LONG lcEntries
  205. )
  206. {
  207. while (--lcEntries >= 0)
  208. LocalFree(lpEntries[lcEntries]);
  209. LocalFree(lpEntries);
  210. } // FreeStringArray
  211. LPTSTR
  212. CanonicalizeAddress(
  213. IN LPTSTR pszAddress
  214. )
  215. {
  216. LPTSTR psz, pWhack;
  217. if (pszAddress[0] == L'\\' && pszAddress[1] == L'\\') {
  218. psz = CopyString(&pszAddress[2]);
  219. if (psz == NULL)
  220. return NULL;
  221. pWhack = wcschr(psz, '\\');
  222. if (pWhack != NULL)
  223. *pWhack = L'\0';
  224. }
  225. else {
  226. psz = CopyString(pszAddress);
  227. if (psz == NULL)
  228. return NULL;
  229. }
  230. _wcslwr(psz);
  231. RASAUTO_TRACE2("CanonicalizeAddress(%S) returns %S", pszAddress, psz);
  232. return psz;
  233. } // CanonicalizeAddress
  234. BOOLEAN
  235. GetOrganization(
  236. IN LPTSTR pszAddr,
  237. OUT LPTSTR pszOrganization
  238. )
  239. {
  240. BOOLEAN fSuccess = FALSE;
  241. TCHAR *pszA, *pszO;
  242. ULONG nDots;
  243. //
  244. // Get the domain and organization name. These
  245. // are the last two components separated by a '.'.
  246. //
  247. for (pszA = pszAddr; *pszA; pszA++);
  248. for (nDots = 0, pszA--; pszA != pszAddr; pszA--) {
  249. if (*pszA == TEXT('.'))
  250. nDots++;
  251. if (nDots == 2)
  252. break;
  253. }
  254. if (nDots == 2 || (pszA == pszAddr && nDots == 1)) {
  255. if (nDots == 2)
  256. pszA++; // skip '.'
  257. for (pszO = pszOrganization; *pszO = *pszA; pszA++, pszO++);
  258. fSuccess = TRUE;
  259. RASAUTO_TRACE2("GetOrganization: org for %S is %S", pszAddr, pszOrganization);
  260. }
  261. return fSuccess;
  262. } // GetOrganization
  263. // Tracing
  264. //
  265. DWORD g_dwRasAutoTraceId = INVALID_TRACEID;
  266. DWORD
  267. RasAutoDebugInit()
  268. {
  269. DebugInitEx("RASAUTO", &g_dwRasAutoTraceId);
  270. return 0;
  271. }
  272. DWORD
  273. RasAutoDebugTerm()
  274. {
  275. DebugTermEx(&g_dwRasAutoTraceId);
  276. return 0;
  277. }
  278. /*
  279. VOID
  280. repareForLongWait(VOID)
  281. {
  282. //
  283. // Unload user-based resources because they
  284. // cannot be held over logout/login sequence.
  285. //
  286. // RegCloseKey(HKEY_CURRENT_USER);
  287. } // PrepareForLongWait
  288. */
  289. #if DBG
  290. VOID
  291. DumpHandles(
  292. IN PCHAR lpString,
  293. IN ULONG a1,
  294. IN ULONG a2,
  295. IN ULONG a3,
  296. IN ULONG a4,
  297. IN ULONG a5
  298. )
  299. {
  300. PSYSTEM_PROCESS_INFORMATION pSystemInfo, pProcessInfo;
  301. ULONG ulHandles;
  302. pSystemInfo = GetSystemProcessInfo();
  303. if (pSystemInfo == NULL)
  304. return;
  305. pProcessInfo = FindProcessByName(pSystemInfo, L"rasman.exe");
  306. if (pProcessInfo == NULL)
  307. return;
  308. DbgPrint(lpString, a1, a2, a3, a4, a5);
  309. DbgPrint(": HANDLES=%d\n", pProcessInfo->HandleCount);
  310. FreeSystemProcessInfo(pSystemInfo);
  311. } // DumpHandles
  312. #endif