Source code of Windows XP (NT5)
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.

408 lines
9.6 KiB

  1. //
  2. // Determine which -- if any -- of these routines are in use
  3. // in sockets project and dump remainder (hopefully whole directory)
  4. // this is ancient stuff, most of which has long been superceded
  5. // They are actually still used.
  6. //
  7. #include "local.h"
  8. #define malloc(x) RtlAllocateHeap( RtlProcessHeap(), 0, (x) )
  9. #define free(p) RtlFreeHeap( RtlProcessHeap(), 0, (p) )
  10. #define WORK_BUFFER_SIZE 1024
  11. char VTCPPARM[] = "\\Registry\\Machine\\System\\CurrentControlSet\\Services\\Tcp\\VParameters";
  12. char NTCPPARM[] = "\\Registry\\Machine\\System\\CurrentControlSet\\Services\\Tcp\\Parameters";
  13. char TCPPARM[] = "\\Registry\\Machine\\System\\CurrentControlSet\\Services\\Tcpip\\Parameters";
  14. char TTCPPARM[] = "\\Registry\\Machine\\System\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Transient";
  15. #ifdef TRACE
  16. void
  17. UnicodePrint(
  18. PUNICODE_STRING UnicodeString
  19. )
  20. /*++
  21. Routine Description:
  22. Print a unicode string.
  23. Arguments:
  24. UnicodeString - pointer to the string.
  25. Return Value:
  26. None.
  27. --*/
  28. {
  29. ANSI_STRING ansiString;
  30. PUCHAR tempbuffer = (PUCHAR) malloc(WORK_BUFFER_SIZE);
  31. ansiString.MaximumLength = WORK_BUFFER_SIZE;
  32. ansiString.Length = 0L;
  33. ansiString.Buffer = tempbuffer;
  34. RtlUnicodeStringToAnsiString(&ansiString,
  35. UnicodeString,
  36. FALSE);
  37. printf("%s", ansiString.Buffer);
  38. free(tempbuffer);
  39. return;
  40. }
  41. #endif
  42. NTSTATUS
  43. SockOpenKey(
  44. PHANDLE HandlePtr,
  45. PUCHAR KeyName
  46. )
  47. /*++
  48. Routine Description:
  49. Arguments:
  50. Return Value:
  51. --*/
  52. {
  53. NTSTATUS status;
  54. STRING keyString;
  55. OBJECT_ATTRIBUTES objectAttributes;
  56. UNICODE_STRING unicodeKeyName;
  57. RtlInitString(&keyString,
  58. KeyName);
  59. (VOID)RtlAnsiStringToUnicodeString(&unicodeKeyName,
  60. &keyString,
  61. TRUE);
  62. #ifdef TRACE
  63. printf("SockOpenKey = ");
  64. UnicodePrint(&unicodeKeyName);
  65. printf("\n");
  66. #endif
  67. memset(&objectAttributes, 0, sizeof(OBJECT_ATTRIBUTES));
  68. InitializeObjectAttributes(&objectAttributes,
  69. &unicodeKeyName,
  70. OBJ_CASE_INSENSITIVE,
  71. NULL,
  72. NULL);
  73. status = NtOpenKey(HandlePtr,
  74. KEY_READ,
  75. &objectAttributes);
  76. RtlFreeUnicodeString(&unicodeKeyName);
  77. #ifdef TRACE
  78. if (!NT_SUCCESS(status)) {
  79. printf("Failed NtOpenKey for %s => %x\n",
  80. KeyName,
  81. status);
  82. }
  83. #endif
  84. return status;
  85. }
  86. NTSTATUS
  87. SockOpenKeyEx(
  88. PHANDLE HandlePtr,
  89. PUCHAR KeyName1,
  90. PUCHAR KeyName2,
  91. PUCHAR KeyName3
  92. )
  93. {
  94. NTSTATUS status;
  95. status = SockOpenKey(HandlePtr, KeyName1);
  96. if (NT_SUCCESS(status) || (KeyName2 == NULL && KeyName3 == NULL)) {
  97. return status;
  98. }
  99. status = SockOpenKey(HandlePtr, KeyName2);
  100. if (NT_SUCCESS(status) || KeyName3 == NULL) {
  101. return status;
  102. }
  103. return SockOpenKey(HandlePtr, KeyName3);
  104. }
  105. NTSTATUS
  106. SockGetSingleValue(
  107. HANDLE KeyHandle,
  108. PUCHAR ValueName,
  109. PUCHAR ValueData,
  110. PULONG ValueType,
  111. ULONG ValueLength
  112. )
  113. /*++
  114. Routine Description:
  115. Arguments:
  116. Return Value:
  117. --*/
  118. {
  119. NTSTATUS status;
  120. ULONG resultLength;
  121. PKEY_VALUE_FULL_INFORMATION keyValueFullInformation;
  122. UNICODE_STRING uValueName;
  123. ANSI_STRING aValueName;
  124. UNICODE_STRING uValueData;
  125. ANSI_STRING aValueData;
  126. #ifdef TRACE
  127. printf("SockGetSingleValue: %s\n", ValueName);
  128. #endif
  129. if ((keyValueFullInformation =
  130. (PKEY_VALUE_FULL_INFORMATION)malloc(WORK_BUFFER_SIZE)) == NULL) {
  131. return(STATUS_NO_MEMORY);
  132. }
  133. RtlZeroMemory(keyValueFullInformation, WORK_BUFFER_SIZE);
  134. uValueName.Length = 0L;
  135. uValueName.MaximumLength = WORK_BUFFER_SIZE;
  136. if ((uValueName.Buffer = (PWSTR)malloc(WORK_BUFFER_SIZE)) == NULL) {
  137. free(keyValueFullInformation);
  138. return(STATUS_NO_MEMORY);
  139. }
  140. aValueName.MaximumLength = WORK_BUFFER_SIZE;
  141. aValueName.Length = (USHORT) strlen(ValueName);
  142. aValueName.Buffer = (PCHAR)ValueName;
  143. RtlAnsiStringToUnicodeString(&uValueName,
  144. &aValueName,
  145. FALSE);
  146. status = NtQueryValueKey(KeyHandle,
  147. &uValueName,
  148. KeyValueFullInformation,
  149. keyValueFullInformation,
  150. WORK_BUFFER_SIZE,
  151. &resultLength);
  152. if (!NT_SUCCESS(status)) {
  153. free(uValueName.Buffer);
  154. free(keyValueFullInformation);
  155. return status;
  156. }
  157. *ValueType = keyValueFullInformation->Type;
  158. if (*ValueType != REG_DWORD && *ValueType != REG_BINARY) {
  159. aValueData.Length = 0L;
  160. if (*ValueType == REG_EXPAND_SZ) {
  161. aValueData.Buffer = (PVOID)uValueName.Buffer;
  162. aValueData.MaximumLength = WORK_BUFFER_SIZE;
  163. } else {
  164. free(uValueName.Buffer);
  165. aValueData.Buffer = (PCHAR)ValueData;
  166. aValueData.MaximumLength = (USHORT)ValueLength;
  167. }
  168. uValueData.Length = (USHORT)keyValueFullInformation->DataLength;
  169. uValueData.MaximumLength = (USHORT)keyValueFullInformation->DataLength;
  170. uValueData.Buffer = (PWSTR)((PCHAR)keyValueFullInformation +
  171. keyValueFullInformation->DataOffset);
  172. RtlUnicodeStringToAnsiString(&aValueData,
  173. &uValueData,
  174. FALSE);
  175. if (*ValueType == REG_EXPAND_SZ) {
  176. resultLength = ExpandEnvironmentStringsA(
  177. aValueData.Buffer,
  178. ValueData,
  179. ValueLength);
  180. free(aValueData.Buffer);
  181. }
  182. } else {
  183. *((PULONG)ValueData) = *((PULONG)((PCHAR)keyValueFullInformation +
  184. keyValueFullInformation->DataOffset));
  185. free(uValueName.Buffer);
  186. }
  187. free(keyValueFullInformation);
  188. return status;
  189. }
  190. NTSTATUS
  191. SockSetSingleValue(
  192. HANDLE KeyHandle,
  193. PUCHAR ValueName,
  194. PUCHAR ValueData,
  195. ULONG ValueType,
  196. ULONG ValueLength
  197. )
  198. /*++
  199. Routine Description:
  200. Arguments:
  201. Return Value:
  202. --*/
  203. {
  204. NTSTATUS status;
  205. UNICODE_STRING uValueName;
  206. ANSI_STRING aValueName;
  207. UNICODE_STRING uData;
  208. ANSI_STRING aData;
  209. BOOL fallocatedUdata = FALSE;
  210. #ifdef TRACE
  211. printf("SockSetSingleValue: %s\n", ValueName);
  212. #endif
  213. uValueName.Length = 0L;
  214. uValueName.MaximumLength = WORK_BUFFER_SIZE;
  215. //
  216. // Convert the key name to unicode.
  217. //
  218. if ((uValueName.Buffer = (PWSTR)malloc(WORK_BUFFER_SIZE)) == NULL) {
  219. return(STATUS_NO_MEMORY);
  220. } else {
  221. aValueName.MaximumLength = WORK_BUFFER_SIZE;
  222. aValueName.Length = (USHORT) strlen(ValueName);
  223. aValueName.Buffer = (PCHAR)ValueName;
  224. RtlAnsiStringToUnicodeString(&uValueName,
  225. &aValueName,
  226. FALSE);
  227. }
  228. if ( ValueType == REG_SZ || ValueType == REG_MULTI_SZ ) {
  229. if ((uData.Buffer = (PWSTR)malloc(WORK_BUFFER_SIZE)) == NULL)
  230. {
  231. free( uValueName.Buffer );
  232. return(STATUS_NO_MEMORY);
  233. }
  234. fallocatedUdata = TRUE;
  235. uData.MaximumLength = WORK_BUFFER_SIZE;
  236. //
  237. // Need to convert the value data from ASCII to unicode
  238. // before writing it to the registry
  239. //
  240. aData.Length = (USHORT)ValueLength;
  241. aData.MaximumLength = aData.Length;
  242. aData.Buffer = ValueData;
  243. RtlAnsiStringToUnicodeString(&uData,
  244. &aData,
  245. FALSE);
  246. } else {
  247. uData.Buffer = (PWCHAR)ValueData;
  248. uData.Length = (USHORT)ValueLength;
  249. }
  250. status = NtSetValueKey(KeyHandle,
  251. &uValueName,
  252. 0,
  253. ValueType,
  254. uData.Buffer,
  255. uData.Length );
  256. if ( fallocatedUdata )
  257. {
  258. free( uData.Buffer );
  259. }
  260. free(uValueName.Buffer);
  261. return status;
  262. }
  263. FILE *
  264. SockOpenNetworkDataBase(
  265. IN char *Database,
  266. OUT char *Pathname,
  267. IN int PathnameLen,
  268. IN char *OpenFlags
  269. )
  270. {
  271. PUCHAR temp;
  272. HANDLE myKey;
  273. NTSTATUS status;
  274. ULONG myType;
  275. //
  276. // Try to open both TCP/IP parameters keys, both old stack and new
  277. // stack.
  278. //
  279. status = SockOpenKeyEx(&myKey, VTCPPARM, NTCPPARM, TCPPARM);
  280. if (!NT_SUCCESS(status)) {
  281. SetLastError( ERROR_CANTOPEN );
  282. return(NULL);
  283. }
  284. if ((temp=malloc(WORK_BUFFER_SIZE))==NULL) {
  285. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  286. NtClose(myKey);
  287. return(NULL);
  288. }
  289. status = SockGetSingleValue(myKey, "DatabasePath", temp, &myType, WORK_BUFFER_SIZE);
  290. NtClose(myKey);
  291. if (!NT_SUCCESS(status)) {
  292. SetLastError( ERROR_CANTREAD );
  293. free(temp);
  294. return(NULL);
  295. }
  296. if ( ((int) (strlen(temp) + strlen(Database) + 2)) > PathnameLen) {
  297. SetLastError( ERROR_INSUFFICIENT_BUFFER );
  298. free(temp);
  299. return(NULL);
  300. }
  301. strcpy(Pathname, temp);
  302. strcat(Pathname, "\\");
  303. strcat(Pathname, Database);
  304. free(temp);
  305. return(fopen(Pathname, OpenFlags));
  306. }