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.

372 lines
11 KiB

  1. //++
  2. //
  3. // Copyright (C) Microsoft Corporation, 1987 - 1999
  4. //
  5. // Module Name:
  6. //
  7. // regutil.c
  8. //
  9. // Abstract:
  10. //
  11. // Queries into network drivers
  12. //
  13. // Author:
  14. //
  15. // Anilth - 4-20-1998
  16. //
  17. // Environment:
  18. //
  19. // User mode only.
  20. // Contains NT-specific code.
  21. //
  22. // Revision History:
  23. //
  24. //--
  25. #include "precomp.h"
  26. #include "ipcfg.h"
  27. //Registry Reading functions, should be able to be replaced by HrReg... standard routines
  28. BOOL ReadRegistryString(HKEY Key, LPCTSTR ParameterName, LPTSTR String, LPDWORD Length)
  29. {
  30. LONG err;
  31. DWORD valueType;
  32. *String = '\0';
  33. err = RegQueryValueEx(Key,
  34. ParameterName,
  35. NULL, // reserved
  36. &valueType,
  37. (LPBYTE)String,
  38. Length
  39. );
  40. if (err == ERROR_SUCCESS) {
  41. ASSERT(valueType == REG_SZ || valueType == REG_MULTI_SZ);
  42. DEBUG_PRINT(("ReadRegistryString(%s): val = \"%s\", type = %d, len = %d\n",
  43. ParameterName,
  44. String,
  45. valueType,
  46. *Length
  47. ));
  48. } else {
  49. DEBUG_PRINT(("ReadRegistryString(%s): err = %d\n", ParameterName, err));
  50. }
  51. return ((err == ERROR_SUCCESS) && (*Length > sizeof('\0')));
  52. }
  53. BOOL ReadRegistryIpAddrString(HKEY Key, LPCTSTR ParameterName, PIP_ADDR_STRING IpAddr)
  54. {
  55. LONG err;
  56. DWORD valueLength = 0;
  57. DWORD valueType;
  58. LPBYTE valueBuffer;
  59. UINT stringCount;
  60. LPTSTR stringPointer;
  61. LPTSTR stringAddress[MAX_STRING_LIST_LENGTH + 1];
  62. UINT i;
  63. err = RegQueryValueEx(Key,
  64. ParameterName,
  65. NULL, // reserved
  66. &valueType,
  67. NULL,
  68. &valueLength
  69. );
  70. if ((err == ERROR_SUCCESS))
  71. {
  72. if((valueLength > 1) && (valueType == REG_SZ)
  73. || (valueLength > 2) && (valueType == REG_MULTI_SZ) )
  74. {
  75. valueBuffer = Malloc(valueLength);
  76. if( NULL == valueBuffer)
  77. {
  78. DebugMessage("Out of Memory!");
  79. err = ERROR_NOT_ENOUGH_MEMORY;
  80. goto Error;
  81. }
  82. err = RegQueryValueEx(Key,
  83. ParameterName,
  84. NULL, // reserved
  85. &valueType,
  86. valueBuffer,
  87. &valueLength
  88. );
  89. if ((err == ERROR_SUCCESS) && (valueLength > 1))
  90. {
  91. stringPointer = valueBuffer;
  92. DEBUG_PRINT(("ReadRegistryIpAddrString(%s): \"%s\", len = %d\n",
  93. ParameterName,
  94. valueBuffer,
  95. valueLength
  96. ));
  97. if( REG_SZ == valueType )
  98. {
  99. stringPointer += strspn(stringPointer, STRING_ARRAY_DELIMITERS);
  100. stringAddress[0] = stringPointer;
  101. stringCount = 1;
  102. while (stringPointer = strpbrk(stringPointer, STRING_ARRAY_DELIMITERS))
  103. {
  104. *stringPointer++ = '\0';
  105. stringPointer += strspn(stringPointer, STRING_ARRAY_DELIMITERS);
  106. stringAddress[stringCount] = stringPointer;
  107. if (*stringPointer)
  108. {
  109. ++stringCount;
  110. }
  111. }
  112. for (i = 0; i < stringCount; ++i)
  113. {
  114. AddIpAddressString(IpAddr, stringAddress[i], "");
  115. }
  116. }
  117. else if( REG_MULTI_SZ == valueType )
  118. {
  119. stringCount = 0;
  120. while(strlen(stringPointer))
  121. {
  122. AddIpAddressString(IpAddr, stringPointer, "");
  123. stringPointer += 1+strlen(stringPointer);
  124. stringCount ++;
  125. }
  126. if( 0 == stringCount )
  127. err = ERROR_PATH_NOT_FOUND;
  128. }
  129. else
  130. {
  131. err = ERROR_PATH_NOT_FOUND;
  132. }
  133. }
  134. else
  135. {
  136. DEBUG_PRINT(("ReadRegistryIpAddrString(%s): err = %d, len = %d\n",
  137. ParameterName,
  138. err,
  139. valueLength
  140. ));
  141. err = ERROR_PATH_NOT_FOUND;
  142. }
  143. Free(valueBuffer);
  144. }
  145. else
  146. {
  147. DEBUG_PRINT(("ReadRegistryIpAddrString(%s): err = %d, type = %d, len = %d\n",
  148. ParameterName,
  149. err,
  150. valueType,
  151. valueLength
  152. ));
  153. err = ERROR_PATH_NOT_FOUND;
  154. }
  155. }
  156. Error:
  157. return (err == ERROR_SUCCESS);
  158. }
  159. BOOL ReadRegistryOemString(HKEY Key, LPWSTR ParameterName, LPTSTR String, LPDWORD Length)
  160. {
  161. LONG err;
  162. DWORD valueType;
  163. DWORD valueLength = 0;
  164. //
  165. // first, get the length of the string
  166. //
  167. *String = '\0';
  168. err = RegQueryValueExW(Key,
  169. ParameterName,
  170. NULL, // reserved
  171. &valueType,
  172. NULL,
  173. &valueLength
  174. );
  175. if ((err == ERROR_SUCCESS) && (valueType == REG_SZ))
  176. {
  177. if ((valueLength <= *Length) && (valueLength > sizeof(L'\0')))
  178. {
  179. UNICODE_STRING unicodeString;
  180. OEM_STRING oemString;
  181. LPWSTR str = (LPWSTR)Malloc(valueLength);
  182. if(NULL == str)
  183. {
  184. assert(FALSE);
  185. DebugMessage("Out of memory!\n");
  186. err = ERROR_NOT_ENOUGH_MEMORY;
  187. goto Error;
  188. }
  189. //
  190. // read the UNICODE string into allocated memory
  191. //
  192. err = RegQueryValueExW(Key,
  193. ParameterName,
  194. NULL,
  195. &valueType,
  196. (LPBYTE)str,
  197. &valueLength
  198. );
  199. if (err == ERROR_SUCCESS) {
  200. //
  201. // convert the UNICODE string to OEM character set
  202. //
  203. RtlInitUnicodeString(&unicodeString, str);
  204. if ( RtlUnicodeStringToOemString(&oemString, &unicodeString, TRUE) == STATUS_SUCCESS)
  205. {
  206. if (oemString.Buffer != NULL)
  207. {
  208. strcpy(String, oemString.Buffer);
  209. DEBUG_PRINT(("ReadRegistryOemString(%ws): val = \"%s\", len = %d\n",
  210. ParameterName,
  211. String,
  212. valueLength
  213. ));
  214. }
  215. RtlFreeOemString(&oemString);
  216. }
  217. } else {
  218. DEBUG_PRINT(("ReadRegistryOemString(%ws): err = %d, type = %d, len = %d\n",
  219. ParameterName,
  220. err,
  221. valueType,
  222. valueLength
  223. ));
  224. }
  225. Free(str);
  226. }
  227. else
  228. {
  229. DEBUG_PRINT(("ReadRegistryOemString(%ws): err = %d, type = %d, len = %d\n",
  230. ParameterName,
  231. err,
  232. valueType,
  233. valueLength
  234. ));
  235. err = !ERROR_SUCCESS;
  236. }
  237. } else {
  238. DEBUG_PRINT(("ReadRegistryOemString(%ws): err = %d, type = %d, len = %d\n",
  239. ParameterName,
  240. err,
  241. valueType,
  242. valueLength
  243. ));
  244. err = !ERROR_SUCCESS;
  245. }
  246. Error:
  247. return (err == ERROR_SUCCESS);
  248. }
  249. BOOL ReadRegistryDword(HKEY Key, LPCTSTR ParameterName, LPDWORD Value)
  250. {
  251. LONG err;
  252. DWORD valueLength;
  253. DWORD valueType;
  254. valueLength = sizeof(*Value);
  255. err = RegQueryValueEx(Key,
  256. ParameterName,
  257. NULL, // reserved
  258. &valueType,
  259. (LPBYTE)Value,
  260. &valueLength
  261. );
  262. if ((err == ERROR_SUCCESS) && (valueType == REG_DWORD) && (valueLength == sizeof(DWORD)))
  263. {
  264. DEBUG_PRINT(("ReadRegistryDword(%s): val = %d, type = %d, len = %d\n",
  265. ParameterName,
  266. *Value,
  267. valueType,
  268. valueLength
  269. ));
  270. } else {
  271. DEBUG_PRINT(("ReadRegistryDword(%d,%s): err = %d\n",
  272. Key, ParameterName, err));
  273. err = !ERROR_SUCCESS;
  274. }
  275. return (err == ERROR_SUCCESS);
  276. }
  277. BOOL
  278. OpenAdapterKey(
  279. const LPTSTR AdapterName,
  280. PHKEY Key
  281. )
  282. {
  283. LONG err;
  284. CHAR keyName[MAX_ADAPTER_NAME_LENGTH + sizeof(TCPIP_PARAMS_INTER_KEY)];
  285. HKEY ServicesKey;
  286. if (NULL == AdapterName) {
  287. DEBUG_PRINT("No Adapter Name");
  288. return FALSE;
  289. }
  290. //
  291. // open the handle to this adapter's TCPIP parameter key
  292. //
  293. strcpy(keyName, TCPIP_PARAMS_INTER_KEY );
  294. strcat(keyName, AdapterName);
  295. err = RegOpenKey(HKEY_LOCAL_MACHINE,SERVICES_KEY,&ServicesKey);
  296. if (err != ERROR_SUCCESS) {
  297. DEBUG_PRINT("Opening Services key failed!\n");
  298. return FALSE;
  299. }
  300. err = RegOpenKey(ServicesKey, keyName, Key );
  301. if( err != ERROR_SUCCESS ){
  302. DEBUG_PRINT(("OpenAdapterKey: RegOpenKey ServicesKey %s, err=%d\n",
  303. keyName, GetLastError() ));
  304. }else{
  305. TRACE_PRINT(("Exit OpenAdapterKey: %s ok\n", keyName ));
  306. }
  307. return (err == ERROR_SUCCESS);
  308. }