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.

414 lines
9.8 KiB

  1. /////////////////////////////////////////////////////////////////////
  2. //
  3. // CopyRight ( c ) 1999 Microsoft Corporation
  4. //
  5. // Module Name: Dnsprov.cpp
  6. //
  7. // Description:
  8. // Implementation of dll exported functions
  9. //
  10. // Author:
  11. // Henry Wang ( henrywa ) March 8, 2000
  12. //
  13. //
  14. //////////////////////////////////////////////////////////////////////
  15. #include "DnsWmi.h"
  16. DEFINE_GUID(CLSID_DNS_SERVER,0x62269fec, 0x7b32, 0x11d2, 0x9a, 0xb7,0x00, 0x00, 0xf8, 0x75, 0xc5, 0xd4);
  17. // { 62269fec-7b32-11d2-9ab7-0000f875c5d4 }
  18. // Count number of objects and number of locks.
  19. long g_cObj=0;
  20. long g_cLock=0;
  21. HMODULE ghModule;
  22. extern DWORD DnsWmiDebugFlag = 0;
  23. extern DWORD DnsLibDebugFlag = 0;
  24. //***************************************************************************
  25. //
  26. // CompileMofFile
  27. //
  28. // Purpose: Automagically compile the MOF file into the WMI repository.
  29. //
  30. // Return: S_OK or error if unable to compile or file MOF.
  31. //
  32. //***************************************************************************
  33. static
  34. SCODE
  35. CompileMofFile(
  36. VOID )
  37. {
  38. SCODE sc = S_OK;
  39. const WCHAR szMofRelativePath[] = L"\\system32\\wbem\\dnsprov.mof";
  40. WCHAR szMofPath[ MAX_PATH + 5 ] = L"";
  41. IMofCompiler * pMofComp = NULL;
  42. HANDLE h;
  43. WBEM_COMPILE_STATUS_INFO Info;
  44. //
  45. // Formulate path of MOF file
  46. //
  47. if ( GetSystemWindowsDirectoryW(
  48. szMofPath,
  49. MAX_PATH - wcslen( szMofRelativePath ) ) == 0 )
  50. {
  51. sc = GetLastError();
  52. goto Done;
  53. }
  54. lstrcatW( szMofPath, szMofRelativePath );
  55. //
  56. // Verify that MOF file exists.
  57. //
  58. h = CreateFileW(
  59. szMofPath,
  60. GENERIC_READ,
  61. FILE_SHARE_READ | FILE_SHARE_WRITE,
  62. NULL,
  63. OPEN_EXISTING,
  64. FILE_ATTRIBUTE_NORMAL,
  65. NULL );
  66. if ( h == INVALID_HANDLE_VALUE )
  67. {
  68. sc = ERROR_FILE_NOT_FOUND;
  69. goto Done;
  70. }
  71. CloseHandle( h );
  72. //
  73. // Load and invoke the MOF compiler.
  74. //
  75. sc = CoCreateInstance(
  76. CLSID_MofCompiler,
  77. NULL,
  78. CLSCTX_INPROC_SERVER,
  79. IID_IMofCompiler,
  80. ( LPVOID * ) &pMofComp );
  81. if ( FAILED( sc ) )
  82. {
  83. goto Done;
  84. }
  85. sc = pMofComp->CompileFile (
  86. ( LPWSTR ) szMofPath,
  87. NULL, // load into namespace specified in MOF file
  88. NULL, // use default User
  89. NULL, // use default Authority
  90. NULL, // use default Password
  91. 0, // no options
  92. 0, // no class flags
  93. 0, // no instance flags
  94. &Info );
  95. //
  96. // Cleanup and return.
  97. //
  98. Done:
  99. if ( pMofComp )
  100. {
  101. pMofComp->Release();
  102. }
  103. return sc;
  104. } // CompileMofFile
  105. //***************************************************************************
  106. //
  107. BOOL
  108. WINAPI
  109. DllMain(
  110. HANDLE hModule,
  111. DWORD dwReason,
  112. LPVOID lpReserved
  113. )
  114. {
  115. DBG_FN( "DllMain" );
  116. #if DBG
  117. DWORD pid = GetCurrentProcessId();
  118. if ( dwReason == DLL_PROCESS_ATTACH )
  119. {
  120. CHAR szBase[ MAX_PATH ];
  121. CHAR szFlagFile[ MAX_PATH + 50 ];
  122. CHAR szLogFile[ MAX_PATH + 50 ];
  123. //
  124. // Initialize debug logging.
  125. //
  126. if ( GetWindowsDirectoryA( szBase, sizeof( szBase ) ) == 0 )
  127. {
  128. return FALSE;
  129. }
  130. strcat( szBase, DNSWMI_DBG_LOG_DIR );
  131. strcpy( szFlagFile, szBase );
  132. strcat( szFlagFile, DNSWMI_DBG_FLAG_FILE_NAME );
  133. sprintf(
  134. szLogFile,
  135. "%s" DNSWMI_DBG_LOG_FILE_BASE_NAME ".%03X.log",
  136. szBase,
  137. pid );
  138. Dns_StartDebug(
  139. 0,
  140. szFlagFile,
  141. &DnsWmiDebugFlag,
  142. szLogFile,
  143. DNSWMI_DBG_LOG_FILE_WRAP );
  144. // Turn off dnslib logging except for basic output controls.
  145. if ( pDnsDebugFlag )
  146. {
  147. pDnsDebugFlag = &DnsLibDebugFlag;
  148. *pDnsDebugFlag = 0x1000000D;
  149. }
  150. IF_DEBUG( START_BREAK )
  151. {
  152. DebugBreak();
  153. }
  154. }
  155. #endif
  156. DNS_DEBUG( INIT, (
  157. "%s: PID %03X reason %d returning TRUE\n", fn, pid, dwReason ));
  158. return TRUE;
  159. } // DllMain
  160. //***************************************************************************
  161. //
  162. // DllCanUnloadNow
  163. //
  164. // Purpose: Called periodically by Ole in order to determine if the
  165. // DLL can be freed.
  166. //
  167. // Return: S_OK if there are no objects in use and the class factory
  168. // isn't locked.
  169. //
  170. //***************************************************************************
  171. STDAPI DllCanUnloadNow(void)
  172. {
  173. DBG_FN( "DllCanUnloadNow" )
  174. SCODE sc;
  175. //It is OK to unload if there are no objects or locks on the
  176. // class factory.
  177. sc=(0L==g_cObj && 0L==g_cLock) ? S_OK : S_FALSE;
  178. DNS_DEBUG( INIT, ( "%s: returning 0x%08x\n", fn, sc ));
  179. return sc;
  180. }
  181. //***************************************************************************
  182. //
  183. // DllRegisterServer
  184. //
  185. // Purpose: Called during setup or by regsvr32.
  186. //
  187. // Return: NOERROR if registration successful, error otherwise.
  188. //***************************************************************************
  189. STDAPI DllRegisterServer(void)
  190. {
  191. SCODE sc = S_OK;
  192. TCHAR szID[ 128 ];
  193. WCHAR wcID[ 128 ];
  194. TCHAR szCLSID[ 128 ];
  195. TCHAR szModule[ MAX_PATH + 1 ];
  196. TCHAR *pName = TEXT("MS_NT_DNS_PROVIDER");
  197. TCHAR *pModel = TEXT("Both");
  198. HKEY hKey1, hKey2;
  199. ghModule = GetModuleHandle(TEXT("Dnsprov"));
  200. // Create the path.
  201. StringFromGUID2(
  202. CLSID_DNS_SERVER,
  203. wcID,
  204. 128);
  205. #ifndef UNICODE
  206. wcstombs(szID, wcID, 128);
  207. #else
  208. _tcscpy(szID, wcID);
  209. #endif
  210. lstrcpy(
  211. szCLSID,
  212. TEXT("Software\\classes\\CLSID\\") );
  213. lstrcat(szCLSID, szID);
  214. // Create entries under CLSID
  215. RegCreateKey(
  216. HKEY_LOCAL_MACHINE,
  217. szCLSID,
  218. &hKey1);
  219. RegSetValueEx(
  220. hKey1,
  221. NULL,
  222. 0,
  223. REG_SZ,
  224. (BYTE *)pName,
  225. lstrlen(pName)+1 );
  226. RegCreateKey(
  227. hKey1,
  228. TEXT("InprocServer32"),
  229. &hKey2);
  230. GetModuleFileName(
  231. ghModule,
  232. szModule,
  233. MAX_PATH );
  234. szModule[ MAX_PATH ] = L'\0';
  235. RegSetValueEx(
  236. hKey2,
  237. NULL,
  238. 0,
  239. REG_SZ,
  240. (BYTE *)szModule,
  241. lstrlen(szModule)+1);
  242. RegSetValueEx(
  243. hKey2,
  244. TEXT("ThreadingModel"),
  245. 0,
  246. REG_SZ,
  247. (BYTE *)pModel, lstrlen(pModel)+1);
  248. CloseHandle(hKey1);
  249. CloseHandle(hKey2);
  250. //
  251. // Compile the MOF file. If this fails, it would be good to
  252. // tell the admin, but I don't have an easy way to do that.
  253. //
  254. CompileMofFile();
  255. return sc;
  256. }
  257. //***************************************************************************
  258. //
  259. // DllUnregisterServer
  260. //
  261. // Purpose: Called when it is time to remove the registry entries.
  262. //
  263. // Return: NOERROR if registration successful, error otherwise.
  264. //***************************************************************************
  265. STDAPI DllUnregisterServer(void)
  266. {
  267. TCHAR szID[128];
  268. WCHAR wcID[128];
  269. TCHAR szCLSID[128];
  270. HKEY hKey;
  271. // Create the path using the CLSID
  272. StringFromGUID2(CLSID_DNS_SERVER, wcID, 128);
  273. #ifndef UNICODE
  274. wcstombs(szID, wcID, 128);
  275. #else
  276. _tcscpy(szID, wcID);
  277. #endif
  278. lstrcpy(szCLSID, TEXT("Software\\classes\\CLSID\\"));
  279. lstrcat(szCLSID, szID);
  280. // First delete the InProcServer subkey.
  281. DWORD dwRet = RegOpenKey(
  282. HKEY_LOCAL_MACHINE,
  283. szCLSID,
  284. &hKey);
  285. if(dwRet == NO_ERROR)
  286. {
  287. RegDeleteKey(
  288. hKey,
  289. TEXT("InProcServer32"));
  290. CloseHandle(hKey);
  291. }
  292. dwRet = RegOpenKey(
  293. HKEY_LOCAL_MACHINE,
  294. TEXT("Software\\classes\\CLSID"),
  295. &hKey);
  296. if(dwRet == NO_ERROR)
  297. {
  298. RegDeleteKey(
  299. hKey,
  300. szID);
  301. CloseHandle(hKey);
  302. }
  303. return NOERROR;
  304. }
  305. //***************************************************************************
  306. //
  307. // DllGetClassObject
  308. //
  309. // Purpose: Called by Ole when some client wants a class factory. Return
  310. // one only if it is the sort of class this DLL supports.
  311. //
  312. //***************************************************************************
  313. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, PPVOID ppv)
  314. {
  315. DBG_FN( "DllGetClassObject" )
  316. HRESULT hr;
  317. CProvFactory *pObj = NULL;
  318. if ( CLSID_DNS_SERVER != rclsid )
  319. {
  320. hr = E_FAIL;
  321. goto Done;
  322. }
  323. pObj = new CProvFactory();
  324. if ( NULL == pObj )
  325. {
  326. hr = E_OUTOFMEMORY;
  327. goto Done;
  328. }
  329. hr = pObj->QueryInterface( riid, ppv );
  330. if ( FAILED( hr ) )
  331. {
  332. delete pObj;
  333. }
  334. Done:
  335. DNS_DEBUG( INIT, ( "%s: returning 0x%08x\n", fn, hr ));
  336. return hr;
  337. }