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.

330 lines
9.0 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: D L L M A I N . C P P
  7. //
  8. // Contents: DLL entry points for netman.dll
  9. //
  10. // Notes:
  11. //
  12. // Author: shaunco 3 Apr 1998
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "nmbase.h"
  18. #include "nmres.h"
  19. #include "ncreg.h"
  20. #include "regkysec.h"
  21. #define INITGUID
  22. #include "nmclsid.h"
  23. #include "..\conman\conman.h"
  24. static const REGENTRY c_RegEntryEAPOL[3] = { { L"System\\CurrentControlSet\\Services\\Eventlog\\Application\\EAPOL", L"EventMessageFile",
  25. REG_EXPAND_SZ, 0, L"%SystemRoot%\\System32\\wzcsvc.dll", NULL, 0, TRUE },
  26. { L"System\\CurrentControlSet\\Services\\Eventlog\\Application\\EAPOL", L"TypesSupported",
  27. REG_DWORD, 0x7, NULL, NULL, 0, FALSE },
  28. { L"Software\\Microsoft\\EAPOL", L"",
  29. REG_NONE, 0, NULL, NULL, 0, FALSE }};
  30. //+---------------------------------------------------------------------------
  31. // DLL Entry Point
  32. //
  33. EXTERN_C
  34. BOOL
  35. WINAPI
  36. DllMain (
  37. HINSTANCE hinst,
  38. DWORD dwReason,
  39. LPVOID pvReserved)
  40. {
  41. if (dwReason == DLL_PROCESS_ATTACH)
  42. {
  43. DisableThreadLibraryCalls (hinst);
  44. InitializeDebugging();
  45. _Module.DllProcessAttach (hinst);
  46. }
  47. else if (dwReason == DLL_PROCESS_DETACH)
  48. {
  49. DbgCheckPrematureDllUnload ("netman.dll", _Module.GetLockCount());
  50. _Module.DllProcessDetach ();
  51. UnInitializeDebugging();
  52. }
  53. return TRUE;
  54. }
  55. //+---------------------------------------------------------------------------
  56. // ServiceMain - Called by the generic service process when starting
  57. // this service.
  58. //
  59. // type of LPSERVICE_MAIN_FUNCTIONW
  60. //
  61. EXTERN_C
  62. VOID
  63. WINAPI
  64. ServiceMain (
  65. DWORD argc,
  66. PWSTR argv[])
  67. {
  68. _Module.ServiceMain (argc, argv);
  69. }
  70. //+---------------------------------------------------------------------------
  71. // DllRegisterServer - Adds entries to the system registry
  72. //
  73. STDAPI
  74. DllRegisterServer ()
  75. {
  76. BOOL fCoUninitialize = TRUE;
  77. HRESULT hr = CoInitializeEx (NULL,
  78. COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED);
  79. if (FAILED(hr))
  80. {
  81. fCoUninitialize = FALSE;
  82. if (RPC_E_CHANGED_MODE == hr)
  83. {
  84. hr = S_OK;
  85. }
  86. }
  87. if (SUCCEEDED(hr))
  88. {
  89. _Module.UpdateRegistryFromResource (IDR_NETMAN, TRUE);
  90. hr = NcAtlModuleRegisterServer (&_Module);
  91. hr = RegisterSvrHelper();
  92. if (fCoUninitialize)
  93. {
  94. CoUninitialize ();
  95. }
  96. }
  97. TraceHr (ttidError, FAL, hr, FALSE, "netman!DllRegisterServer");
  98. return hr;
  99. }
  100. //+---------------------------------------------------------------------------
  101. // DllUnregisterServer - Removes entries from the system registry
  102. //
  103. STDAPI
  104. DllUnregisterServer ()
  105. {
  106. _Module.UpdateRegistryFromResource (IDR_NETMAN, FALSE);
  107. _Module.UnregisterServer ();
  108. return S_OK;
  109. }
  110. //+---------------------------------------------------------------------------
  111. // RegisterSvrHelper - Sets up registry keys and other information.
  112. //
  113. STDAPI
  114. RegisterSvrHelper()
  115. {
  116. HRESULT hr = S_OK;
  117. DWORD dwErr = NO_ERROR;
  118. PSID psidLocalService = NULL;
  119. PSID psidNetworkService = NULL;
  120. SID_IDENTIFIER_AUTHORITY sidAuth = SECURITY_NT_AUTHORITY;
  121. hr = CreateEAPOLKeys();
  122. if(SUCCEEDED(hr))
  123. {
  124. if (AllocateAndInitializeSid(&sidAuth, 1, SECURITY_LOCAL_SERVICE_RID, 0, 0, 0, 0, 0, 0, 0, &psidLocalService))
  125. {
  126. hr = SetKeySecurity(2, psidLocalService, KEY_READ_WRITE_DELETE);
  127. if (SUCCEEDED(hr))
  128. {
  129. if (AllocateAndInitializeSid(&sidAuth, 1, SECURITY_NETWORK_SERVICE_RID, 0, 0, 0, 0, 0, 0, 0, &psidNetworkService))
  130. {
  131. hr = SetKeySecurity(2, psidNetworkService, KEY_READ_WRITE_DELETE);
  132. FreeSid(psidNetworkService);
  133. }
  134. else
  135. {
  136. dwErr = GetLastError();
  137. }
  138. }
  139. FreeSid(psidLocalService);
  140. }
  141. else
  142. {
  143. dwErr = GetLastError();
  144. }
  145. if (dwErr)
  146. {
  147. hr = HRESULT_FROM_WIN32(dwErr);
  148. }
  149. }
  150. return hr;
  151. }
  152. //+---------------------------------------------------------------------------
  153. // CreateEAPOLKeys - Creates the entries required by EAPOL
  154. //
  155. STDAPI
  156. CreateEAPOLKeys()
  157. {
  158. HRESULT hr = S_OK;
  159. HKEY hKey = NULL;
  160. // Need to write: HKLM\System\CurrentControlSet\Services\Eventlog\System\EAPOL
  161. // EventMessageFile - REG_EXPAND_SZ: %SystemRoot%\System32\netman.dll
  162. // TypesSupported - REG_DWORD 0x07
  163. for (int i = 0; i < celems(c_RegEntryEAPOL); i++)
  164. {
  165. if (hKey == NULL)
  166. {
  167. DWORD dwRet;
  168. dwRet = RegCreateKey(HKEY_LOCAL_MACHINE, c_RegEntryEAPOL[i].strKeyName, &hKey);
  169. hr = HRESULT_FROM_WIN32(dwRet);
  170. }
  171. if (SUCCEEDED(hr))
  172. {
  173. switch (c_RegEntryEAPOL[i].dwType)
  174. {
  175. case REG_NONE:
  176. break;
  177. case REG_SZ:
  178. case REG_MULTI_SZ:
  179. case REG_EXPAND_SZ:
  180. hr = HrRegSetValueEx(hKey, c_RegEntryEAPOL[i].strValueName, c_RegEntryEAPOL[i].dwType, reinterpret_cast<BYTE*>(c_RegEntryEAPOL[i].strValue), (wcslen(c_RegEntryEAPOL[i].strValue) + 1) * sizeof(WCHAR));
  181. break;
  182. case REG_BINARY:
  183. hr = HrRegSetValueEx(hKey, c_RegEntryEAPOL[i].strValueName, c_RegEntryEAPOL[i].dwType, reinterpret_cast<BYTE*>(c_RegEntryEAPOL[i].pbValue), c_RegEntryEAPOL[i].dwBinLen);
  184. break;
  185. case REG_DWORD:
  186. hr = HrRegSetValueEx(hKey, c_RegEntryEAPOL[i].strValueName, c_RegEntryEAPOL[i].dwType, reinterpret_cast<const BYTE*>(&(c_RegEntryEAPOL[i].dwValue)), sizeof(c_RegEntryEAPOL[i].dwValue));
  187. break;
  188. }
  189. }
  190. if (!c_RegEntryEAPOL[i].fMoreOnKey)
  191. {
  192. RegCloseKey(hKey);
  193. hKey = NULL;
  194. }
  195. }
  196. return hr;
  197. }
  198. //+---------------------------------------------------------------------------
  199. // CreateEAPOLKeys - Creates the entries required by EAPOL
  200. //
  201. STDAPI
  202. SetKeySecurity(DWORD dwKeyIndex, PSID psidUserOrGroup, ACCESS_MASK dwAccessMask)
  203. {
  204. HRESULT hr = S_OK;
  205. HKEY hKey = NULL;
  206. DWORD dwErr = 0;
  207. CRegKeySecurity rkSec;
  208. hr = rkSec.RegOpenKey(HKEY_LOCAL_MACHINE, c_RegEntryEAPOL[dwKeyIndex].strKeyName);
  209. if (SUCCEEDED(hr))
  210. {
  211. hr = rkSec.GetKeySecurity();
  212. if (SUCCEEDED(hr))
  213. {
  214. hr = rkSec.GrantRightsOnRegKey(psidUserOrGroup, dwAccessMask, KEY_ALL);
  215. if (SUCCEEDED(hr))
  216. {
  217. hr = rkSec.SetKeySecurity();
  218. }
  219. }
  220. rkSec.RegCloseKey();
  221. }
  222. else
  223. {
  224. dwErr = GetLastError();
  225. hr = HRESULT_FROM_WIN32(dwErr);
  226. }
  227. return hr;
  228. }
  229. HRESULT
  230. GetClientAdvises(LPWSTR** pppszAdviseUsers, LPDWORD pdwCount)
  231. {
  232. HRESULT hr = S_OK;
  233. if ( (!pppszAdviseUsers) || (!pdwCount) )
  234. {
  235. return E_POINTER;
  236. }
  237. CConnectionManager *pConMan = const_cast<CConnectionManager*>(CConnectionManager::g_pConMan);
  238. if (!pConMan)
  239. {
  240. return E_UNEXPECTED;
  241. }
  242. DWORD dwTotalLength = 0;
  243. DWORD dwNumItems = 0;
  244. list<tstring> NameList;
  245. pConMan->g_fInUse = TRUE;
  246. pConMan->Lock();
  247. for (ITERUSERNOTIFYMAP iter = pConMan->m_mapNotify.begin(); iter != pConMan->m_mapNotify.end(); iter++)
  248. {
  249. tstring szName = iter->second->szUserName;
  250. NameList.push_back(szName);
  251. dwTotalLength += sizeof(WCHAR) * (szName.length() + 1);
  252. dwNumItems++;
  253. }
  254. pConMan->Unlock();
  255. pConMan->g_fInUse = FALSE;
  256. if (!dwNumItems)
  257. {
  258. *pppszAdviseUsers = NULL;
  259. *pdwCount = 0;
  260. return S_FALSE;
  261. }
  262. DWORD dwAllocSize = dwNumItems * sizeof(LPCWSTR) + dwTotalLength;
  263. *pppszAdviseUsers = reinterpret_cast<LPWSTR *>(CoTaskMemAlloc(dwAllocSize));
  264. if (!*pppszAdviseUsers)
  265. {
  266. return E_OUTOFMEMORY;
  267. }
  268. LPWSTR pszEndString = reinterpret_cast<LPWSTR>(reinterpret_cast<LPBYTE>(*pppszAdviseUsers) + dwAllocSize);
  269. // First string in the structure
  270. LPWSTR pszCurrentString = reinterpret_cast<LPWSTR>(reinterpret_cast<LPBYTE>(*pppszAdviseUsers) + (sizeof(LPWSTR) * dwNumItems));
  271. // First pointer in the structure
  272. LPWSTR* lppArray = *pppszAdviseUsers;
  273. for (list<tstring>::const_iterator iterName = NameList.begin(); iterName != NameList.end(); iterName++)
  274. {
  275. *lppArray = pszCurrentString;
  276. wcscpy(pszCurrentString, iterName->c_str());
  277. lppArray++;
  278. pszCurrentString += (iterName->size() + 1);
  279. Assert(pszCurrentString <= pszEndString);
  280. }
  281. *pdwCount = dwNumItems;
  282. return hr;
  283. }